《数据库存图片(3页).doc》由会员分享,可在线阅读,更多相关《数据库存图片(3页).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 class Form1 : Form public F
2、orm1() 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; op
3、en_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;
4、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 Sql
5、Parameter(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 =
6、 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;