《数据库存图片.doc》由会员分享,可在线阅读,更多相关《数据库存图片.doc(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流数据库存图片【精品文档】第 3 页using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Data.SqlClient;namespace DemoFileStream public partial cla
2、ss Form1 : Form public Form1() InitializeComponent(); /把?图?片? 二t进?制? 数y据Y量? 存?入?SQL private void button1_Click(object sender, EventArgs e) pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;/使1图?片?能完整?显?示? OpenFileDialog open_file_dialog = new OpenFileDialog(); open_file_dialog.InitialDirectory
3、= C:Users小?刚?Desktop; open_file_dialog.Filter = jpg *.jpg|*.jpg; if (open_file_dialog.ShowDialog() = DialogResult.OK) string filePath = open_file_dialog.FileName; pictureBox1.Image = Image.FromFile(filePath);/图?片?显?示?在框内 FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read); byte bu
4、ffer=new bytefs.Length; fs.Read(buffer,0,(int)fs.Length); /把? 流fs 存?入? 数y组buffer 中D fs.Close(); SqlConnection con = new SqlConnection(server=(local);database=db_BookManage;Uid=sa;pwd=sql); string cmd_str = insert into tb_admin values(22,22,1,buffer); SqlCommand cmd = new SqlCommand(cmd_str,con); Sql
5、Parameter para = new SqlParameter(buffer,SqlDbType.Image); para.Value = buffer; cmd.Parameters.Add(para); con.Open(); cmd.ExecuteNonQuery(); con.Close(); /从SQL中D 读取?以?二t进?制? 方?式?存?入?的? 图?片? private void button2_Click(object sender, EventArgs e) string con_str = select * from tb_admin where AdminName
6、=22; SqlConnection con = new SqlConnection(server=(local);database=db_BookManage;Uid=sa;pwd=sql); SqlCommand cmd = new SqlCommand(con_str,con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); MemoryStream ms = new MemoryStream(byte)drAdminPhoto); Image image = Image.FromStream(ms,true); dr.Close(); con.Close(); pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.Image = image;