using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PhotoSQL {publicpartialclass Form1 : Form {public Form1() { InitializeComponent(); }string sqlCon ="";privatevoid button1_Click(object sender, EventArgs e) {string url =@"C:\Users\jinwei\Desktop\tiger.jpg";byte[] dd = GetPictureData(url); ImgToDB(dd);//保存到数据库中 }privatevoid button2_Click(object sender, EventArgs e) { ImgDBTo(); }////// 保存到数据库//////privatevoid ImgToDB(byte[] imgBytesIn) {try { SqlConnection con=new SqlConnection("server=SDSC2-1,1433;uid=sa;pwd=jinwei;database=web"); con.Open(); SqlCommand cmd=new SqlCommand("insert into aaaa (img) values( @Image ) ;", con); cmd.Parameters.Add("@Image", SqlDbType.Image); cmd.Parameters["@Image"].Value = imgBytesIn; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("图片上传成功"); }catch { MessageBox.Show("您选择的图片不能被读取或文件类型不对!","错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }////// 从数据库中 图片 并且保存到C盘///publicvoid ImgDBTo() {byte[] MyData =newbyte[0];using (SqlConnection conn =new SqlConnection("server=SDSC2-1,1433;uid=sa;pwd=jinwei;database=web")) { conn.Open(); SqlCommand cmd=new SqlCommand(); cmd.Connection= conn; cmd.CommandText="select * from aaaa"; SqlDataReader sdr= cmd.ExecuteReader(); sdr.Read();object o = sdr["img"]; MyData= (byte[])sdr["img"];//读取第一个图片的位流int ArraySize = MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限 FileStream fs=new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData,0, ArraySize); fs.Close();//-- 写入到c:\00.jpg。 conn.Close(); Console.WriteLine("读取成功");//查看硬盘上的文件 } }////// 根据路径将图片转换成 byte[]/////////publicbyte[] GetPictureData(string imagepath) {/**/////