数据库存图片.doc
如有侵权,请联系网站删除,仅供学习与交流数据库存图片【精品文档】第 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 class 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 = "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 buffer=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); SqlParameter 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='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)dr"AdminPhoto"); Image image = Image.FromStream(ms,true); dr.Close(); con.Close(); pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.Image = image;