Storing Data into the database
Create a table Photo which has 2 columns Id and Photo .
id int
photo varbinary(MAX)
Code
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
SqlConnection con;
private void buttonStore_Click(object sender, EventArgs e)
{
byte[] image;
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
// string fileName = @"c:\images.jpg";
string fileName = op.FileName;
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\stud.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
// Open File and Read Into Byte Array
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
BinaryReader reader = new BinaryReader(fs);
image = reader.ReadBytes((int)fs.Length);
fs.Close();
}
string insertSql = "INSERT INTO Photo (Photo) VALUES (@Photo)";
SqlCommand cmd = new SqlCommand(insertSql, con);
cmd.Parameters.AddWithValue("@Photo", image);
int i = cmd.ExecuteNonQuery();
MessageBox.Show ("inserted");
con.Close();
}
To retrieve the stored Image
private void buttonRetrieve_Click(object sender, EventArgs e)
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\stud.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
string selectSql = "SELECT photo FROM Photo WHERE ID =18";
SqlCommand cmd = new SqlCommand(selectSql, con);
byte[] storedImage = (byte[])cmd.ExecuteScalar();
System.Drawing.Image newImage;
using (MemoryStream stream = new MemoryStream(storedImage))
{
newImage = System.Drawing.Image.FromStream(stream);
}
// Display to make sure code wor
pictureBox1.Image = newImage;
}
Create a table Photo which has 2 columns Id and Photo .
id int
photo varbinary(MAX)
Code
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
SqlConnection con;
private void buttonStore_Click(object sender, EventArgs e)
{
byte[] image;
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
// string fileName = @"c:\images.jpg";
string fileName = op.FileName;
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\stud.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
// Open File and Read Into Byte Array
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
BinaryReader reader = new BinaryReader(fs);
image = reader.ReadBytes((int)fs.Length);
fs.Close();
}
string insertSql = "INSERT INTO Photo (Photo) VALUES (@Photo)";
SqlCommand cmd = new SqlCommand(insertSql, con);
cmd.Parameters.AddWithValue("@Photo", image);
int i = cmd.ExecuteNonQuery();
MessageBox.Show ("inserted");
con.Close();
}
To retrieve the stored Image
private void buttonRetrieve_Click(object sender, EventArgs e)
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\stud.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
string selectSql = "SELECT photo FROM Photo WHERE ID =18";
SqlCommand cmd = new SqlCommand(selectSql, con);
byte[] storedImage = (byte[])cmd.ExecuteScalar();
System.Drawing.Image newImage;
using (MemoryStream stream = new MemoryStream(storedImage))
{
newImage = System.Drawing.Image.FromStream(stream);
}
// Display to make sure code wor
pictureBox1.Image = newImage;
}
0 comments:
Post a Comment