电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

介绍如何将图片存入数据库

介绍如何将图片存入数据库_第1页
1/23
介绍如何将图片存入数据库_第2页
2/23
介绍如何将图片存入数据库_第3页
3/23
本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下: 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=(local);uid=sa;pwd=;database=db_05"); con.Open(); SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con); com.Parameters.Add("ImageList", SqlDbType.Image); com.Parameters["ImageList"].Value = imagebytes; com.ExecuteNonQuery(); con.Close(); } } 本实例主要介绍如何从数据库中把图片读出来。实现本实例主要是利用 SqlDataReader从数据库中把Image字段值读出来,赋给一个 byte[]字节数组,然后使用 MemoryStream类与Bitmap把图片读取出来。主要代码如下: private void button1_Click(object sender, EventArgs e) { byte[] imagebytes = null; //打开数据库 SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05"); con.Open(); SqlCommand com = new SqlCommand("select top 1* from tb_09", con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read()) { imagebytes = (byte[])dr.GetValue(1); } dr.Close(); com.Clone(); con.Close(); MemoryStream ms = new MemoryStream(imagebytes); Bitmap bmpt = new Bitmap(ms); pictureBox1.Image = bmpt; } 本实例主要介绍如何只允许输入指定图片格式。用 OpenFileDialog控件打开图片文件,只要将 OpenFileDialog控件的 Filter属性指定为特定的图片格式即可。例如,打开.bmp文件的图片,主要代...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

介绍如何将图片存入数据库

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部