c winform 上传文件到mysql_VS 2010中WinForm开发(C#)—图片上传到数据库与显示(sql server 2008)...

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());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值