Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL
What will be the Query to insert the image into the table for Sql Server 2008
Posted 13-Dec-12 1:34am
Comments
01010RAJ at 13-Dec-12 7:36am
   
oh so u want directly query to,select image form system and store them into database.
 
So wait,I will also wait?
Perhaps it's possible?I don't know
OriginalGriff at 13-Dec-12 7:39am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
SQL does not know about Images, so you nbeed to feed it a sequence of bytes - where they come form is a matter for the application calling SQL server, and exactly how you do it will depend on the language it is written in.
Use the "Improve question" widget to edit your question and provide better information.
Milind_T at 13-Dec-12 8:13am
   
Why are you reposting question. http://www.codeproject.com/Questions/508376/InsertplusImageplusinplusSqlplusDatabase
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check this code sample
protected void Button1_Click(object sender, EventArgs e)
    {
        //  'using System.IO' 
      
        FileStream fs = new FileStream("C:\\Files\\draft.jpg",FileMode.Open,FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        byte[] photo = br.ReadBytes((int)fs.Length);
 
        br.Close();
        fs.Close();
 
        //  'using System.Data.SqlClient'
                     
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ContestConnection"].ConnectionString);
 
        SqlCommand addpic = new SqlCommand(  "INSERT INTO userTable ("+" img_data) "+" VALUES(@img)",myConnection);
            
        addpic.Parameters.Add("@img",SqlDbType.Image, photo.Length).Value = photo;
        
        myConnection.Open();
        
        addpic.ExecuteNonQuery();
        myConnection.Close();
         
        }
 
If you use FileUploadControl , then the code will be something like this
 
protected void btnUpload_Click(object sender, EventArgs e)
 
{
 
//Create a new filestream object based on the file chosen in the FileUpload control

FileStream fs = new FileStream(ImageUploadToSQL.PostedFile.FileName, FileMode.Open, FileAccess.Read);
 
//Create a binary reader object to read the binary contents of the file to upload

BinaryReader br = new BinaryReader(fs);
 
//dump the bytes read into a new byte variable named image

byte[] image = br.ReadBytes((int)fs.Length);
 
//close the binary reader

br.Close();
 
//close the filestream

fs.Close();
 
//Now that we've read the chosen file into a byte variable we can work with,

//stick the contents of that variable into the SQL Server database

//Create a SQL Server connection variable

SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BlobsConnectionString"].ConnectionString);
 
//Create a SQL Server command variable

SqlCommand addimage = new SqlCommand("INSERT INTO Images (" + " Image, ImageName)" + " VALUES(@Image, @ImageName)", myConnection);
 
//Assign our image variable and filename variable to the parameters of the SQL Command

addimage.Parameters.Add("@Image", SqlDbType.Image, image.Length).Value = image;
 
addimage.Parameters.Add("@ImageName", SqlDbType.NVarChar).Value = txtFileName.Text;
 
//open the SQL connection and run the command to insert image and image name to the database

myConnection.Open();
 
addimage.ExecuteNonQuery();
 
myConnection.Close();
 
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Check my post in this following link.Click here
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In Sql table take column of varbinary(max) type:
and after that while inserting, first convert the image to byte array using this code

public byte[] ConvertImageToByteArray(FileUploadField fuImage)
{
    byte[] ImageByteArray;
    try
    {
        MemoryStream ms = new MemoryStream(fuImage.FileBytes);
        ImageByteArray = ms.ToArray();
        return ImageByteArray;
    }
    catch (Exception ex)
    {
        return null;
    }
}
 
Pass this byte array as parameter to your insert function as follow
public static long insertImage(string ID, byte[] Image)
     {
 
         long Id = 0;
         SqlConnection conn = null;
         try
         {
             string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
             conn = new SqlConnection(strConn);
             SqlCommand cmd = new SqlCommand();
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Connection = conn;
             cmd.CommandText = "Your_Sp_Name_Here";
             cmd.Parameters.Add("@ID", SqlDbType.VarChar, 30).Value = ID;
             cmd.Parameters.Add("@Image", SqlDbType.Image, 0).Value = Image;
             conn.Open();
 
             Id = Convert.ToInt16(cmd.ExecuteScalar());
             conn.Close();
 

 
         }
         catch (Exception ex)
         {
 
         }
 
         finally
         {
             if (conn != null && conn.State != ConnectionState.Closed)
                 conn.Close();
         }
 

         return Id;
     }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 355
1 BillWoodruff 334
2 George Jonsson 279
3 Sergey Alexandrovich Kryukov 243
4 Gihan Liyanage 225
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100