//存图片到DB
private void button1_Click(object sender, EventArgs e)
{
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
ofdSelectPic.ShowDialog();
string f = ofdSelectPic.FileName;
ms = ofdSelectPic.OpenFile();
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
conn.Open();
string sqlstring = "insert into ttt(image) values(@img)";
SqlCommand cmd = new SqlCommand(sqlstring, conn);
cmd.Parameters.Add("@img", SqlDbType.Image, picbyte.Length).Value = picbyte;
cmd.ExecuteNonQuery();
conn.Close();
}
//由数据取图片
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
conn.Open();
string strSql = "select image from ttt ";
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
Image image = Image.FromStream(ms);
sdr.Close();
conn.Close();
pictureBox1.Image = image;
}