日期:2014-05-19  浏览次数:20934 次

怎么把MemoryStream的数据,存到SqlServer的varbinary中呀?
怎么把MemoryStream的数据,存到SqlServer的varbinary中呀?谢谢大家!!

------解决方案--------------------
参考下面的代码:

private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog(this) == DialogResult.OK)
{
Image img = Image.FromFile(of.FileName);
MemoryStream ms = new MemoryStream();
img.Save(ms, img.RawFormat);
string sql = "insert into Pictures (Name, Photo) Values(@Name, @Photo) ";
SqlConnection cn = new SqlConnection();
cn.ConnectionString = this.txtConnect.Text;
cn.Open();
SqlCommand cm = cn.CreateCommand();
cm.CommandText = sql;
cm.Parameters.Add( "@Name ", SqlDbType.VarChar, 500).Value=of.FileName;
cm.Parameters.Add( "@Photo ", SqlDbType.Image).Value = ms.GetBuffer();
cm.ExecuteNonQuery();
}
}

------解决方案--------------------
string strSql = "insert into table(content)values(@content); "
SqlConn .. conn;
SqlCommand cmd;..

System.IO.MemoryStream m = new System.IO.MemoryStream();
byte[] byteBuffer = new byte[m.Length];
m.Read(byteBuffer,0,byteBuffer.Length);
cmd.Parameters.Add( "@content ",byteBuffer);

cmd.写入数据库
------解决方案--------------------
SqlDataReader reader =conn.ExecuteReader( "select Photo from Table ");

byte[] buffer= reader.GetSqlBytes(0).Buffer;

MemoryStream mem=new MemoryStream(buffer);