1
2
3
4
讲座信息
中心优势
国际优势:长期为日本输送软件人才。
师资优势:雄厚的师资力量。
就业优势:专属软件园区学友所用。
环境优势:一流校区,一流设备。
成本优势:同样的知识不同样的价格。
位置优势:毗邻首都CBD经济圈。
联系我们
☎:400-812-9800
☎:010-58411039
☎:0316-5996777
☎:0316-5996977



您现在的位置: 北大青鸟燕郊校区 >> 青鸟学社 >> 学习部 >> 正文

图片保存到数据库和图片从数据库中读取

日期:2010-7-26 9:00:01    来源:本站原创    作者:学社   

  using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace WindowsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";//打开的文件格式 if (openFileDialog1.ShowDialog() == DialogResult.OK) { string fullpath = openFileDialog1.FileName;//文件路径 FileStream fs = new FileStream(fullpath, FileMode.Open);//创建读写文件对象 byte[] imagebytes = new byte[fs.Length];// BinaryReader br = new BinaryReader(fs);//将文件转换成二进制 imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length)); //打开数据库 SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=picDB"); con.Open(); SqlCommand com = new SqlCommand("insert into picTb values(@pic)", con); com.Parameters.Add("pic", SqlDbType.Image); com.Parameters["pic"].Value = imagebytes; com.ExecuteNonQuery(); con.Close(); } } private void button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=picDB"); string str = "select top 1 pic from pictb"; try { SqlCommand command = new SqlCommand(str, con); con.Open(); SqlDataReader picture = command.ExecuteReader(); if (picture.Read()) { MessageBox.Show(picture.GetValue(0).ToString()); MemoryStream ms = new MemoryStream((byte[])picture.GetValue(0)); pictureBox1.Image = Image.FromStream(ms); picture.Close(); } } catch (Exception ex) { MessageBox.Show("错误"); } finally { con.Close(); } } private void Form1_Load(object sender, EventArgs e) { } } }

  • 上一篇文章:
  • 下一篇文章: 没有了