private void Writer()
{
string MyConnectionString="data source=xxx;initial catalog=xx;password=;user id=sa;";
SqlConnection MyConnection = new SqlConnection(MyConnectionString);
Stream st=new FileStream("c:\\kp.bmp",FileMode.Open,FileAccess.Read);
int t=1;
byte[] b_img=new byte[st.Length];
st.Read(b_img,0,(int)st.Length);
MessageBox.Show(st.Length.ToString());
st.Close();
SqlCommand MyCommand=new SqlCommand("insert into cs(id,img) values(@t,@b_img)",MyConnection);
SqlParameter MyParameterI=new SqlParameter("@t",SqlDbType.Int);
MyParameterI.Value=t;
MyCommand.Parameters.Add(MyParameterI);SqlParameter MyParameterB=new SqlParameter("@b_img",SqlDbType.Image);
MyParameterB.Value=b_img;
MyCommand.Parameters.Add(MyParameterB);MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}

解决方案 »

  1.   

    注:字段img是Image类型它可以放20亿个字节。
        中间一行MessageBox.Show(st.Length.ToString());多余,我测试用的。
      

  2.   

    string MyConnectionString="data source=xxx;initial catalog=xx;password=;user id=sa;";
    SqlConnection MyConnection = new SqlConnection(MyConnectionString);
    Stream st=new FileStream("c:\\kp.bmp",FileMode.Open,FileAccess.Read);
    int t=1;
    byte[] b_img=new byte[st.Length];
    st.Read(b_img,0,(int)st.Length);
    MessageBox.Show(st.Length.ToString());
    st.Close();
    SqlCommand MyCommand=new SqlCommand("insert into cs(id,img) values(@t,@b_img)",MyConnection);
    SqlParameter MyParameterI=new SqlParameter("@t",SqlDbType.Int);
    MyParameterI.Value=t;
    MyCommand.Parameters.Add(MyParameterI);SqlParameter MyParameterB=new SqlParameter("@b_img",SqlDbType.Image);
    MyParameterB.Value=b_img;
    MyCommand.Parameters.Add(MyParameterB);MyConnection.Open();
    MyCommand.ExecuteNonQuery();
    MyConnection.Close();