title: Write image to SQL Server CE
body: <p><img src="https://i.stack.imgur.com/YpgoA.jpg" alt="enter image description here"></p>
  
  <p>I have a button on a form application which is supposed to write an image to a SQL Server database which has been selected using <code>OpenFileDialog</code>.</p>
  
  <p>What I am trying to accomplish is to tailor this code to write the selected image to a SQL Server Compact Edition that I have set up, the db is called <code>TestImage.sdf</code> and the table is called <code>test-table</code>. Currently when I click the store button I get the </p>
  
  <blockquote>
    <p>Cannot locate SQL server error</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>private void updatedata() 
  {
     //use filestream object to read the image.
     //read to the full length of image to a byte array.
     //add this byte as an oracle parameter and insert it into database. 
     try     
     {
         //proceed only when the image has a valid path        
         if (imagename != "")    
         { 
           FileStream fs;
           fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
           //a byte array to read the image
           byte[] picbyte = new byte[fs.Length];     
           fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));  
           fs.Close();
           //open the database using odp.net and insert the data  
           string connstr = @"Data Source=.;Initial Catalog=TestImage; 
                              Persist Security Info=True;User ID=sa";  
  
           SqlConnection conn = new SqlConnection(connstr); 
           conn.Open();  
           string query; 
           query = "insert into test_table(id_image,pic) values(" +
           textBox1.Text + "," + " @pic)";   
           SqlParameter picparameter = new SqlParameter(); 
           picparameter.SqlDbType = SqlDbType.Image;   
           picparameter.ParameterName = "pic"; 
           picparameter.Value = picbyte;                
           SqlCommand cmd = new SqlCommand(query, conn);  
           cmd.Parameters.Add(picparameter);
           cmd.ExecuteNonQuery();
           MessageBox.Show("Image Added");  
           cmd.Dispose();  
           conn.Close();
           conn.Dispose();
           Connection();
         }
     }   
     catch (Exception ex)     
     { 
        MessageBox.Show(ex.Message); 
     }  
  }  
  </code></pre>