using System;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;using System.Data.SqlClient;namespacephotoSC
{public partial classForm1 : Form
{
SqlConnection conn;publicForm1()
{
InitializeComponent();
}private void button1_Click(objectsender, EventArgs e)
{
openFileDialog1.InitialDirectory=“C:\\”;
openFileDialog1.Filter= “图片文件 (*.jpg)|*.jpg”;
openFileDialog1.FilterIndex= 1;
openFileDialog1.RestoreDirectory= true;
openFileDialog1.Multiselect= true;if (openFileDialog1.ShowDialog() ==DialogResult.OK)
{
textBox1.Text=openFileDialog1.FileName;
}
}private void button2_Click(objectsender, EventArgs e)
{
Save(PhotoToArray(textBox1.Text.ToString()));
Form1 f1= newForm1();
f1.ShowDialog();//不在任务栏显示//this.ShowInTaskbar = false;//窗体透明
this.Opacity = 0; //后面的取值0~100 ,0表示透明
}private byte[] PhotoToArray(stringpath)
{
FileStream stream= newFileStream(path, FileMode.Open, FileAccess.Read);byte[] bufferPhoto = new byte[stream.Length];
stream.Read(bufferPhoto,0, Convert.ToInt32(stream.Length));
stream.Flush();
stream.Close();returnbufferPhoto;
}private void Save(byte[] image)
{
conn= newSqlConnection(SQL.conStr);string sql =“insert into Photo(photo_Info,photo_Id) values(@photo,@photo_id)”;
SqlParameter param= newSqlParameter();
param= newSqlParameter(“@photo”, SqlDbType.Image);
param.Value=image;
SqlParameter param1= newSqlParameter();
param1= newSqlParameter(“@photo_id”, SqlDbType.Int);
param1.Value=textBox2.Text.ToString();
SqlCommand commd= newSqlCommand(sql, conn);
commd.Parameters.Add(param);
commd.Parameters.Add(param1);try{
conn.Open();
commd.ExecuteNonQuery();
MessageBox.Show(“您已经把图片成功的插入数据库!”);
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}finally{
conn.Close();
}
}private void button3_Click(objectsender, EventArgs e)
{
conn= newSqlConnection(SQL .conStr);string strSQL = “Select [photo_Info] From [Photo] Where [photo_Id]=(@photo)”;
SqlParameter param= newSqlParameter();
param= newSqlParameter(“@photo”, SqlDbType.Int);
param.Value=comboBox1.Text.ToString();
SqlCommand cmd= newSqlCommand(strSQL,conn);
cmd.Parameters.Add(param);
conn.Open();
System.Data.SqlClient.SqlDataReader reader=cmd.ExecuteReader();try{
reader.Read();
MemoryStream ms= new MemoryStream((byte[])reader["photo_Info"]);
System.Drawing.Image image= System.Drawing.Image.FromStream(ms, true);this.pictureBox1.Image =image;
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}finally{
conn.Close();
}
}private void Form1_Load(objectsender, EventArgs e)
{//TODO: 这行代码将数据加载到表“exampelDataSet.Photo”中。您可以根据需要移动或删除它。
this.photoTableAdapter.Fill(this.exampelDataSet.Photo);int photoid =Convert.ToInt32( comboBox1.Text.Trim().ToString());
}
}
}