<转>简单明了的实现图片到数据库的存储

 1private void btnSaveToDB_Click(object sender, EventArgs e)
 2{
 3  SqlConnection conn = new SqlConnection(@"data source=MICROSOF-D0EE36;uid=sa;pwd=freebsd;database=test");
 4  conn.Open();
 5  SqlCommand cmd = new SqlCommand("insert into test(testpic) values (@i)", conn);
 6  byte[] ib = new Byte[60000];
 7  if (this.openFileDialog1.ShowDialog() ==DialogResult.OK)
 8   {
 9       FileStream fs = new FileStream(this.openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
10      fs.Read(ib, 0, 60000);
11      cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
12      cmd.Parameters["@i"].Value = ib;
13      cmd.ExecuteNonQuery();
14      MessageBox.Show("图片内容以存入数据库!");
15
16    }
17                conn.Close();
18 }
19
20        private void btnTakePicToImage_Click(object sender, EventArgs e)
21        {
22            SqlConnection conn = new SqlConnection(@"data source=MICROSOF-D0EE36;uid=sa;pwd=freebsd;database=test");
23            conn.Open();
24            SqlCommand cmd = new SqlCommand("select * from test", conn);
25            SqlDataReader reader = cmd.ExecuteReader();
26            if (reader.Read())
27            {
28                MemoryStream buf = new MemoryStream((byte[])reader[1]);
29                Image image = Image.FromStream(buf, true);
30                pictureBox1.Image = image;
31            }
32            else
33                MessageBox.Show("没有任何数据!");
34
35        }
36

 

你可能感兴趣的:(FreeBSD)