Answered by:
how to store image to database?

Question
-
Hi friendz..
how can i store image to database. which datatype i have to use for image. and how can make browse button. for windows application?
please any one give one sample
thankz
faisalSaturday, January 9, 2010 7:09 AM
Answers
-
Hi,
in ur db use image DataTye.
//Insert image to DB
byte[] byteImage= System.IO.FIle.ReadAllFileBytes(openDialog1.FileName);
string querry="Insert into testTable (ImageName, ImageByte) Values ('"+fileName+"',"@ImgByte")";
cmd.Parameters.AddValue("@ImgByte", byteImage);
sqlServer.ExecuteCMD(querry);
//Retrive image from DB
string querry ="Select ImageName, ImageByte From testTable";
sqlServer.ExcuteCMD(querry);
byte[] byteImage = dr["ImageByte "];
//you can do 2 ways
1) System.IO.FIle.WriteAllBytes("path", byteImage);
read the image from the path and display
2) put it into memory stream and bind it to image.
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:31 AM
Saturday, January 9, 2010 7:34 AM -
hi,
Make sure in ur DB imageColumn should have image DataTye<br/> DataRow dr; dr = imadedbDataSet.Tables["tblImage"].NewRow(); dr[0] = textBox1.text; dr[1] = System.IO.File.ReadAllBytes(openDialog1.FileName); imadedbDataSet.Tables["tblImage"].Rows.Add(dr); tblImageTableAdapter.Update(imadedbDataSet);
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:33 AM
Saturday, January 9, 2010 8:40 AM -
hi,
DataTable dt; DataRow dr; dt = imadedbDataSet.Tables["tblImage"]; dr = dt.NewRow(); dr[0] = System.IO.File.ReadAllBytes(openFileDialog1.FileName); dr[1] = System.IO.File.ReadAllBytes(openFileDialog2.FileName); dt.Rows.Add(dr); tblImageTableAdapter.Update(imadedbDataSet);
for dr[0] use "openFileDialog1.FileName " and dr[1] use "openFileDialog2.FileName "
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:31 AM
Saturday, January 9, 2010 9:20 AM -
Think About it:
- private void SaveImage()
- {
- SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;" + "integrated security=sspi;database=Teste_One");
- //Create Command object
- SqlCommand nonqueryCommand = thisConnection.CreateCommand();
- try {
- // Open Connection
- thisConnection.Open();
- Interaction.MsgBox("Connection Opened");
- // Create INSERT statement with named parameters
- nonqueryCommand.CommandText = "INSERT INTO Table_OBS VALUES(@OBS, @PictureMe, @FavarMe)";
- // Add Parameters to Command Parameters collection
- nonqueryCommand.Parameters.Add("@OBS", SqlDbType.NVarChar, 50);
- nonqueryCommand.Parameters.Add("@PictureMe", SqlDbType.Image);
- nonqueryCommand.Parameters.Add("@FavarMe", SqlDbType.NVarChar, 50);
- // Prepare command for repeated execution
- //nonqueryCommand.Prepare()
- // Data to be inserted
- nonqueryCommand.Parameters("@OBS").Value = this.TextBox1.Text;
- nonqueryCommand.Parameters("@PictureMe").Value = photo;
- nonqueryCommand.Parameters("@FavarMe").Value = this.TextBox2.Text;
- int ok = nonqueryCommand.ExecuteNonQuery();
- Interaction.MsgBox(ok);
- }
- catch (SqlException ex) {
- // Display error
- Interaction.MsgBox("Error: " + ex.ToString());
- }
- finally {
- // Close Connection
- thisConnection.Close();
- Interaction.MsgBox("Connection Closed");
- }
- }
Just Be Humble Malange!- Marked as answer by liurong luo Wednesday, January 13, 2010 11:32 AM
Saturday, January 9, 2010 7:34 PM
All replies
-
you can use BLOB datatype to store imagesPlease check the below site for code..
hope this helped..
Narayanan Dayalan -------- Please "Mark As Answer", if my answer works well with ur QuerySaturday, January 9, 2010 7:18 AM -
Hi Abdul,
Check this link for storing images to database
Happy Coding, RDRajaSaturday, January 9, 2010 7:24 AM -
thanks for your reply
you given the link for vb and asp. i need for visual C#.net.
can i use image datatype for store images?
give me one example for store image to the database
thanks
faisalSaturday, January 9, 2010 7:29 AM -
Hi,
in ur db use image DataTye.
//Insert image to DB
byte[] byteImage= System.IO.FIle.ReadAllFileBytes(openDialog1.FileName);
string querry="Insert into testTable (ImageName, ImageByte) Values ('"+fileName+"',"@ImgByte")";
cmd.Parameters.AddValue("@ImgByte", byteImage);
sqlServer.ExecuteCMD(querry);
//Retrive image from DB
string querry ="Select ImageName, ImageByte From testTable";
sqlServer.ExcuteCMD(querry);
byte[] byteImage = dr["ImageByte "];
//you can do 2 ways
1) System.IO.FIle.WriteAllBytes("path", byteImage);
read the image from the path and display
2) put it into memory stream and bind it to image.
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:31 AM
Saturday, January 9, 2010 7:34 AM -
here is the link for C#hope this helped....
Narayanan Dayalan -------- Please "Mark As Answer", if my answer works well with ur QuerySaturday, January 9, 2010 8:00 AM -
hi Nagarjuna Dilip,thanks for answer
i am using below code for string to save to the table.
can you give the sample to store the image to table like belocode.
DataTable dt;DataRow dr;
dt=imadedbDataSet.Tables[
"tblImage"];
dr = dt.NewRow();
dr[0] = textBox1.text;
dr[1] = textBox1.Text;
dt.Rows.Add(dr);
tblImageTableAdapter.Update(imadedbDataSet);
Saturday, January 9, 2010 8:03 AM -
hi,
Make sure in ur DB imageColumn should have image DataTye<br/> DataRow dr; dr = imadedbDataSet.Tables["tblImage"].NewRow(); dr[0] = textBox1.text; dr[1] = System.IO.File.ReadAllBytes(openDialog1.FileName); imadedbDataSet.Tables["tblImage"].Rows.Add(dr); tblImageTableAdapter.Update(imadedbDataSet);
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:33 AM
Saturday, January 9, 2010 8:40 AM -
thanks for your reply
i want to uloade two images. and i have created two texboxes and two browse button.
see below code.
it saved same images in both filed
i want to store first image in image1 field that os dr[0] and secong in image2 field that is dr[1]
but when is store it the image1 and image2 has same imaeg.
DataTable
dt;
DataRow dr;
dt = imadedbDataSet.Tables[
"tblImage"];
dr = dt.NewRow();
dr[0] = System.IO.
File.ReadAllBytes(openFileDialog1.FileName);
dr[1] = System.IO.
File.ReadAllBytes(openFileDialog1.FileName);
dt.Rows.Add(dr);
tblImageTableAdapter.Update(imadedbDataSet);
Saturday, January 9, 2010 9:07 AM -
hi,
DataTable dt; DataRow dr; dt = imadedbDataSet.Tables["tblImage"]; dr = dt.NewRow(); dr[0] = System.IO.File.ReadAllBytes(openFileDialog1.FileName); dr[1] = System.IO.File.ReadAllBytes(openFileDialog2.FileName); dt.Rows.Add(dr); tblImageTableAdapter.Update(imadedbDataSet);
for dr[0] use "openFileDialog1.FileName " and dr[1] use "openFileDialog2.FileName "
Nagarjuna Dilip- Proposed as answer by Yasser Zamani - Mr. Help Saturday, January 9, 2010 8:29 PM
- Marked as answer by liurong luo Wednesday, January 13, 2010 11:31 AM
Saturday, January 9, 2010 9:20 AM -
Think About it:
- private void SaveImage()
- {
- SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;" + "integrated security=sspi;database=Teste_One");
- //Create Command object
- SqlCommand nonqueryCommand = thisConnection.CreateCommand();
- try {
- // Open Connection
- thisConnection.Open();
- Interaction.MsgBox("Connection Opened");
- // Create INSERT statement with named parameters
- nonqueryCommand.CommandText = "INSERT INTO Table_OBS VALUES(@OBS, @PictureMe, @FavarMe)";
- // Add Parameters to Command Parameters collection
- nonqueryCommand.Parameters.Add("@OBS", SqlDbType.NVarChar, 50);
- nonqueryCommand.Parameters.Add("@PictureMe", SqlDbType.Image);
- nonqueryCommand.Parameters.Add("@FavarMe", SqlDbType.NVarChar, 50);
- // Prepare command for repeated execution
- //nonqueryCommand.Prepare()
- // Data to be inserted
- nonqueryCommand.Parameters("@OBS").Value = this.TextBox1.Text;
- nonqueryCommand.Parameters("@PictureMe").Value = photo;
- nonqueryCommand.Parameters("@FavarMe").Value = this.TextBox2.Text;
- int ok = nonqueryCommand.ExecuteNonQuery();
- Interaction.MsgBox(ok);
- }
- catch (SqlException ex) {
- // Display error
- Interaction.MsgBox("Error: " + ex.ToString());
- }
- finally {
- // Close Connection
- thisConnection.Close();
- Interaction.MsgBox("Connection Closed");
- }
- }
Just Be Humble Malange!- Marked as answer by liurong luo Wednesday, January 13, 2010 11:32 AM
Saturday, January 9, 2010 7:34 PM -
Hi friendz..
how can i store image to database. which datatype i have to use for image. and how can make browse button. for windows application?
please any one give one sample
thankz
faisal- public static byte[] GetPhoto(string filePath)
- {
- FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- BinaryReader reader = new BinaryReader(stream);
- byte[] photo = reader.ReadBytes(stream.Length);
- reader.Close();
- stream.Close();
- return photo;
- }
Just Be Humble Malange!Saturday, January 9, 2010 7:34 PM -
Hi friendz..
how can i store image to database. which datatype i have to use for image. and how can make browse button. for windows application?
please any one give one sample
thankz
faisal- private void LoadPicture()
- {
- using (OpenFileDialog OpenFileDialog1 = new OpenFileDialog()) {
- OpenFileDialog1.InitialDirectory = "c:\\Libraries";
- OpenFileDialog1.Filter = "Access files (*.jpg)|*.jpg";
- OpenFileDialog1.FilterIndex = 0;
- OpenFileDialog1.RestoreDirectory = true;
- if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
- string FileName = OpenFileDialog1.FileName;
- photo = GetPhoto(FileName);
- Interaction.MsgBox(photo.LongLength);
- PictureBox1.Image = Image.FromFile(FileName);
- }
- }
- }
Just Be Humble Malange!Saturday, January 9, 2010 7:35 PM -
Nice work, Malange.
The line numbers are pretty, but they make it difficult to copy and paste your code into Visual Studio. Try it!
Mark the best replies as answers. "Fooling computers since 1971."Saturday, January 9, 2010 8:35 PM -
Nice work, Malange.
The line numbers are pretty, but they make it difficult to copy and paste your code into Visual Studio. Try it!
Mark the best replies as answers. "Fooling computers since 1971."Thanks, next time.
Just Be Humble Malange!Saturday, January 9, 2010 10:11 PM