求winform中 上传照片 到 mysql 的代码 winform中 上传照片到mysql 数据库中。照片直接保存到数据库 photo字段中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlConnection conn; private void button1_Click(object sender, EventArgs e) { string connecString; Stream imgStream; string filePath; string strSql; SqlCommand cmd; int intImgSize; connecString = "data Source=127.0.0.1;user id=sa;password=5566;initial catalog=xin;"; conn = new SqlConnection(connecString); try { openFile.ShowDialog(); imgStream = openFile.OpenFile(); strSql = @"insert into test(picture,discription) values(@picture,@disc) "; cmd = new SqlCommand(strSql, conn); conn.Open(); if (conn.State == ConnectionState.Closed) { conn.Open(); } openFile.ShowDialog(); imgStream = openFile.OpenFile(); intImgSize = (int)imgStream.Length; byte[] imgContex = new byte[intImgSize]; imgStream.Read(imgContex, 0, intImgSize); cmd.Parameters.Add("@picture", SqlDbType.Binary); cmd.Parameters["@picture"].Value = imgContex; cmd.Parameters.Add("@disc", SqlDbType.VarChar); cmd.Parameters["@disc"].Value = "new picture"; int intResult = cmd.ExecuteNonQuery(); if (intResult<0) { MessageBox.Show("更新失敗"); } } catch (Exception ex) { MessageBox.Show("Error:" + "\n" + ex.Message); } finally { if(conn.State==ConnectionState.Open) conn.Close(); conn.Dispose(); } } } openFile是一個openFileDialog控件 private void btnSave_Click(object sender, EventArgs e) { if (imageFilePath.Trim().Length == 0) { MessageBox.Show("你没有选择图片","注意",MessageBoxButtons.OK,MessageBoxIcon.Warning); return; } try { using (SqlConnection conn = new SqlConnection(connString)) { string comdText = "UPDATE dbo.章立民研究室 SET 相片 = @BLOBData WHERE 身份证号码 = @Id "; SqlCommand comd = new SqlCommand(comdText, conn); comd.Parameters.Add("@Id", SqlDbType.NVarChar, 18).Value = this.lstId.SelectedItem.ToString(); //建立一个MemoryStream对象 MemoryStream ms = new MemoryStream(); //将PictureBox中的影像以JPEG图形格式存入MemoryStream对象中 Bitmap myBitmap = new Bitmap(this.pirPoto.BackgroundImage); myBitmap.Save(ms, ImageFormat.Jpeg); byte[] byteBLOBData = new byte[(int)ms.Length]; ms.Position = 0; ms.Read(byteBLOBData, 0, (int)ms.Length); ms.Close(); comd.Parameters.Add("@BLOBData", SqlDbType.VarBinary, byteBLOBData.Length).Value = byteBLOBData; conn.Open(); comd.ExecuteNonQuery(); } } catch { } } .net有没这个功能??? 公式自动计算 .net windows 开发什么书好 TreeView.ExpandAll() 怎么让他停止。 多表填充水晶报表的问题,求能实际解决问题的方案!!知道者请进...... C#,如何接收系统关机消息? 用控件数据集邦定的问题!! 100分,简单问题,顺便散分 C#属性 帮我看看这段代码为什么不能在鼠标按下处加载指定的位图? 求vs.net的注册码 实现3个窗体之间的打开,关闭 关于IQueryable类型取数据的问题
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
private void button1_Click(object sender, EventArgs e)
{
string connecString;
Stream imgStream;
string filePath;
string strSql;
SqlCommand cmd;
int intImgSize;
connecString = "data Source=127.0.0.1;user id=sa;password=5566;initial catalog=xin;";
conn = new SqlConnection(connecString);
try
{
openFile.ShowDialog();
imgStream = openFile.OpenFile();
strSql = @"insert into test(picture,discription) values(@picture,@disc) ";
cmd = new SqlCommand(strSql, conn);
conn.Open();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
openFile.ShowDialog();
imgStream = openFile.OpenFile();
intImgSize = (int)imgStream.Length;
byte[] imgContex = new byte[intImgSize];
imgStream.Read(imgContex, 0, intImgSize);
cmd.Parameters.Add("@picture", SqlDbType.Binary);
cmd.Parameters["@picture"].Value = imgContex;
cmd.Parameters.Add("@disc", SqlDbType.VarChar);
cmd.Parameters["@disc"].Value = "new picture";
int intResult = cmd.ExecuteNonQuery();
if (intResult<0)
{
MessageBox.Show("更新失敗");
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + "\n" + ex.Message);
}
finally
{
if(conn.State==ConnectionState.Open)
conn.Close();
conn.Dispose();
}
}
}
{
if (imageFilePath.Trim().Length == 0)
{
MessageBox.Show("你没有选择图片","注意",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
} try
{
using (SqlConnection conn = new SqlConnection(connString))
{
string comdText = "UPDATE dbo.章立民研究室 SET 相片 = @BLOBData WHERE 身份证号码 = @Id "; SqlCommand comd = new SqlCommand(comdText, conn); comd.Parameters.Add("@Id", SqlDbType.NVarChar, 18).Value = this.lstId.SelectedItem.ToString(); //建立一个MemoryStream对象
MemoryStream ms = new MemoryStream(); //将PictureBox中的影像以JPEG图形格式存入MemoryStream对象中
Bitmap myBitmap = new Bitmap(this.pirPoto.BackgroundImage);
myBitmap.Save(ms, ImageFormat.Jpeg); byte[] byteBLOBData = new byte[(int)ms.Length]; ms.Position = 0;
ms.Read(byteBLOBData, 0, (int)ms.Length);
ms.Close(); comd.Parameters.Add("@BLOBData", SqlDbType.VarBinary, byteBLOBData.Length).Value = byteBLOBData; conn.Open();
comd.ExecuteNonQuery();
}
}
catch
{
}
}