1

在此处输入图像描述

我在表单应用程序上有一个按钮,该按钮应该将图像写入已使用OpenFileDialog.

我想要完成的是定制此代码以将所选图像写入我设置的 SQL Server Compact Edition,调用 db 并调用TestImage.sdftest-table。目前,当我单击商店按钮时,我得到了

找不到 SQL 服务器错误

我的代码:

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); 
   }  
}  
4

2 回答 2

2

SQL Server Compact Edition 需要其自己的一组 ADO.NET 类才能运行。

您不能使用用于 SQL Server 的类SqlConnection,例如SqlCommand等,但您应该使用相应SqlCeConnectionSqlCeCommand等......

using  System.Data.SqlServerCe;

private void updatedata() 
{
     try     
     {
         if (imagename != "")    
         { 
            FileStream fs;
            byte[] picbyte;
            using(fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read))
            {
                //a byte array to read the image
                picbyte = new byte[fs.Length];     
                fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));  
            }
            string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"
            string query = "insert into test_table(id_image,pic) " + 
                           "values(@id, @pic)";   
             using(SqlCeConnection conn = new SqlCeConnection(connstr))
             using(SqlCeCommand cmd = new SqlCeCommand(query, conn))
             {
                conn.Open();  
                SqlCeParameter picparameter = new SqlCeParameter(); 
                picparameter.SqlDbType = SqlDbType.Image;   
                picparameter.ParameterName = "pic"; 
                picparameter.Value = picbyte;                
                cmd.Parameters.Add(picparameter);
                SqlCeParameter idparameter = new SqlCeParameter(); 
                idparameter.SqlDbType = SqlDbType.Int;   
                idparameter.ParameterName = "id"; 
                idparameter.Value = textBox1.Text;                
                cmd.Parameters.Add(idparameter);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Image Added");  
             }
        }   
     }
      .....

我已经修改了连接字符串以添加您要使用的数据库的文件名(假设它在您的应用程序的同一目录中)并适应 SQL Server CE 使用的标准文本。

然后我添加了适当的using 语句以确保正确处理连接和命令。

最后,我还为该id_image字段添加了参数(请注意,如果此字段是一个身份列,则不应尝试在此处插入任何内容)

于 2013-06-09T21:21:32.600 回答
1

TestImage.sdf您的连接字符串中 没有丢失吗?

连接字符串应该是

string connstr = @"Data Source=TestImage.sdf;Initial Catalog=TestImage; Persist Security Info=True;User ID=sa";  

编辑:
另请注意,SQL Server CE 每行限制约 8kB,因此图像必须非常小。

对于小于 200x200 的位图(当然您可以将其他图像格式加载到位图中),我使用了以下代码

public static byte[] GetBytesFromBmp(Bitmap bmp)
{
            //initialize quality array
            Int64[] qual = { 100L, 90L, 80L, 70L, 60L, 50L, 40L, 30L, 20L, 10L };
            ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
            var myEncoder = Encoder.Quality;
            var encParameters = new EncoderParameters(1);
            int qualindex = 0;

            byte[] ret;
            do
            {
                //generate for selected quality
                var param = new EncoderParameter(myEncoder, qual[qualindex]);
                qualindex++;
                encParameters.Param[0] = param;

                var str = new MemoryStream();
                bmp.Save(str, jpgEncoder, encParameters);
                ret = str.ToArray();

            } while (ret.Length > 7900 && qualindex < 8); //lower quality and check if the size doesn't exceeded SQL Server CE limitations

            return ret;
} 
于 2013-06-09T21:28:49.737 回答