locked
how to store image to database? RRS feed

  • 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

    faisal
    Saturday, 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
    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
    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
    Saturday, January 9, 2010 9:20 AM
  • Think About it:
    1. private void SaveImage()
    2. {
    3.     SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;" + "integrated security=sspi;database=Teste_One");
    4.    
    5.     //Create Command object
    6.     SqlCommand nonqueryCommand = thisConnection.CreateCommand();
    7.    
    8.     try {
    9.         // Open Connection
    10.         thisConnection.Open();
    11.         Interaction.MsgBox("Connection Opened");
    12.        
    13.        
    14.         // Create INSERT statement with named parameters
    15.         nonqueryCommand.CommandText = "INSERT INTO Table_OBS VALUES(@OBS, @PictureMe, @FavarMe)";
    16.        
    17.         // Add Parameters to Command Parameters collection
    18.         nonqueryCommand.Parameters.Add("@OBS", SqlDbType.NVarChar, 50);
    19.         nonqueryCommand.Parameters.Add("@PictureMe", SqlDbType.Image);
    20.         nonqueryCommand.Parameters.Add("@FavarMe", SqlDbType.NVarChar, 50);
    21.        
    22.         // Prepare command for repeated execution
    23.         //nonqueryCommand.Prepare()
    24.        
    25.         // Data to be inserted
    26.         nonqueryCommand.Parameters("@OBS").Value = this.TextBox1.Text;
    27.         nonqueryCommand.Parameters("@PictureMe").Value = photo;
    28.         nonqueryCommand.Parameters("@FavarMe").Value = this.TextBox2.Text;
    29.        
    30.         int ok = nonqueryCommand.ExecuteNonQuery();
    31.            
    32.         Interaction.MsgBox(ok);
    33.     }
    34.     catch (SqlException ex) {
    35.        
    36.         // Display error
    37.         Interaction.MsgBox("Error: " + ex.ToString());
    38.     }
    39.     finally {
    40.         // Close Connection
    41.         thisConnection.Close();
    42.            
    43.         Interaction.MsgBox("Connection Closed");
    44.     }
    45. }

    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 images


    Narayanan Dayalan -------- Please "Mark As Answer", if my answer works well with ur Query
    Saturday, January 9, 2010 7:18 AM
  • Saturday, 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

    faisal
    Saturday, 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
    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 Query
    Saturday, 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
    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
    Saturday, January 9, 2010 9:20 AM
  • Think About it:
    1. private void SaveImage()
    2. {
    3.     SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;" + "integrated security=sspi;database=Teste_One");
    4.    
    5.     //Create Command object
    6.     SqlCommand nonqueryCommand = thisConnection.CreateCommand();
    7.    
    8.     try {
    9.         // Open Connection
    10.         thisConnection.Open();
    11.         Interaction.MsgBox("Connection Opened");
    12.        
    13.        
    14.         // Create INSERT statement with named parameters
    15.         nonqueryCommand.CommandText = "INSERT INTO Table_OBS VALUES(@OBS, @PictureMe, @FavarMe)";
    16.        
    17.         // Add Parameters to Command Parameters collection
    18.         nonqueryCommand.Parameters.Add("@OBS", SqlDbType.NVarChar, 50);
    19.         nonqueryCommand.Parameters.Add("@PictureMe", SqlDbType.Image);
    20.         nonqueryCommand.Parameters.Add("@FavarMe", SqlDbType.NVarChar, 50);
    21.        
    22.         // Prepare command for repeated execution
    23.         //nonqueryCommand.Prepare()
    24.        
    25.         // Data to be inserted
    26.         nonqueryCommand.Parameters("@OBS").Value = this.TextBox1.Text;
    27.         nonqueryCommand.Parameters("@PictureMe").Value = photo;
    28.         nonqueryCommand.Parameters("@FavarMe").Value = this.TextBox2.Text;
    29.        
    30.         int ok = nonqueryCommand.ExecuteNonQuery();
    31.            
    32.         Interaction.MsgBox(ok);
    33.     }
    34.     catch (SqlException ex) {
    35.        
    36.         // Display error
    37.         Interaction.MsgBox("Error: " + ex.ToString());
    38.     }
    39.     finally {
    40.         // Close Connection
    41.         thisConnection.Close();
    42.            
    43.         Interaction.MsgBox("Connection Closed");
    44.     }
    45. }

    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
    1. public static byte[] GetPhoto(string filePath)
    2. {
    3.     FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    4.     BinaryReader reader = new BinaryReader(stream);
    5.    
    6.     byte[] photo = reader.ReadBytes(stream.Length);
    7.    
    8.     reader.Close();
    9.     stream.Close();
    10.    
    11.     return photo;
    12. }

    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
    1. private void LoadPicture()
    2. {
    3.    
    4.     using (OpenFileDialog OpenFileDialog1 = new OpenFileDialog()) {
    5.         OpenFileDialog1.InitialDirectory = "c:\\Libraries";
    6.         OpenFileDialog1.Filter = "Access files (*.jpg)|*.jpg";
    7.         OpenFileDialog1.FilterIndex = 0;
    8.         OpenFileDialog1.RestoreDirectory = true;
    9.         if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
    10.             string FileName = OpenFileDialog1.FileName;
    11.             photo = GetPhoto(FileName);
    12.             Interaction.MsgBox(photo.LongLength);
    13.                
    14.             PictureBox1.Image = Image.FromFile(FileName);
    15.            
    16.         }
    17.     }
    18. }

    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. I have the code in VB I had to convert it into C Sharp.

    Thanks, next time.

    Just Be Humble Malange!
    Saturday, January 9, 2010 10:11 PM