byte[] byteImage = new byte[Image.Length]
string strSql="";
strSql="insert into Image (image_data) values ()";问题:values()  应该怎么写;

解决方案 »

  1.   

    strSql="insert into Image (image_data) values (@image_data)";
    command.Parameters.AddWithValue("@image_data",byteImage ); 
      

  2.   

    //保存图片: 
                       SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");  
                    conn.Open();  
                    SqlCommand cmd = new SqlCommand("insert into image values(@i)", conn);  
                    byte[] ib = new byte[60000];  
                    FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read);  
                    fs.Read(ib, 0, 60000);  
                    cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);  
                    cmd.Parameters["@i"].Value = ib;  
                    cmd.ExecuteNonQuery();  
                    conn.Close();  
                    MessageBox.Show("保存成功");  
    //显示图片: 
                  SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");  
                conn.Open();  
                SqlCommand cmd = new SqlCommand("select image1 from image", conn);  
                SqlDataReader reader = cmd.ExecuteReader();  
                reader.Read();  
                while (reader.Read())  
                {  
                    for (int i = 0; i  < reader.FieldCount; i++)  
                    {  
                        MemoryStream buf = new MemoryStream((byte[])reader[i]);  
                        Image image = Image.FromStream(buf,true);  
                        this.pictureBox1.Image = image;  
                    }  
                }  
      

  3.   

    参见类似的做法
    http://dotnet.aspx.cc/article/9154bc99-df64-4e2d-b096-26c99ce464be/read.aspx
      

  4.   

    不用SqlCommand  应该怎么写。
    因为 连库的代码 都已经封装好了
    只能传string
      

  5.   

    有两个选择,1是上面说的,修改sqlcommand的调用方式,这样效率最高,
    2,可以使用自己的编码方式,吧byte[]转换成字符串,然后保存到数据库的Text类型字段里,读取时反过来就可以,例如:
    byte[] b = new byte[xxx]; 
    //read data to byte[]
    string s = StringEncode(b);//这里举个例,直接转换成hex编码或转换成base64也可以,
    //save to database
    //read data
    string s = ......//读出byte[]数据。
    byte[] b = StringDecode(s);//反操作,吧hex编码或base64编码再转换成byte[]
      

  6.   

    byte[] buffer = new byte[stream.Length];   
    stream.Read(buffer, 0, (int)stream.Length);   
    stream.Close();   SqlCommand cmd = new SqlCommand("", sqlConn);   
    cmd.Parameters.Add("@photo", SqlDbType.Image).Value = buffer; 
    http://topic.csdn.net/u/20100419/20/5f087418-0d24-4bc5-a652-a7a4d0fc95d9.html
      

  7.   

    如果必须作为string保存的话,使用Convert.ToBase64String方法将buffer中的数据转换为string再保存。
      

  8.   

    String.Format("insert into Image (image_data) values ({0})",byteImage[0]);
      

  9.   

    String strSql=String.Format("insert into Image (image_data) values ({0})",byteImage[0]);
    - -