private void button1_Click(object sender, RoutedEventArgs e)
{
System.IO.FileStream fs = new System.IO.FileStream(@"C:\Users\fei\Pictures\IMG_20120621_212522.jpg",System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] str = new byte[fs.Length];
fs.Read(str, 0, str.Length);
fs.Close();
InsertImage(str);
}
private void InsertImage(byte[] str)
{
string constr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText="insert into ImageTable(photo) values(@ImageValue)";
SqlParameter par = new SqlParameter("@ImageValue", System.Data.SqlDbType.Image);
par.Value = str;
cmd.Parameters.Add(par);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("插入成功!");
}
}
}
可以用这个代码试一下,我自己写的试了一下是可以的。
|