关于图片上传数据库,下载到picturebox里的操作。
来源:互联网 发布:js event对象为null 编辑:程序博客网 时间:2024/05/16 10:28
1.首先创建数据
id yqid zlbh zlmc zltp(资料图片img格式)
lab_yqgl_da
我的数据库及字段。
2.在窗体的代码模块顶部插入 using 语句:(操作图片后面添加,请查看!)
3.添加 Button按钮,上传图片,还要加一个openFileDialog1,代码如下
private void bt_upload_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
//读取图片
private string filename;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (pictureBox1.Image != null)
{
this.pictureBox1.Image = null;
}
//上传图片到pictureBox1
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
Bitmap bmp = new Bitmap(fs);
this.pictureBox1.Image = bmp;
//图像适合图片框的大小
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
fs.Close();
filename = openFileDialog1.FileName.ToString();
}
4.向数据库里添加图片。
在Button添加按钮里
//id自动生成
string sqlid = "select max(id) from lab_yqgl_da";
SqlCommand cmd = new SqlCommand(sqlid, alluse.Class1.conn1);
string id1 = Convert.ToString(cmd.ExecuteScalar());
if (id1 == "")
{
id = "1";
}
else
{
int da = Int32.Parse(id1) + 1;
id = da.ToString();
}
//
string zlbh = this.comboBox1.Text.Trim().ToString();
string zlmc = this.comboBox2.Text.Trim().ToString();
string strsql = "insert into lab_yqgl_da(id,yqid,zlbh,zlmc,zltp) values('" + id + "','" +
yqgl_da_add_yqid + "','" + zlbh + "','" + zlmc + "',@img)";
SqlCommand mycmd = new SqlCommand(strsql, alluse.Class1.conn1);
//加入图片
SqlParameter prm = new SqlParameter("@img", SqlDbType.Image);
MemoryStream ms = new MemoryStream();
System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
prm.Value = ms.GetBuffer();
mycmd.Parameters.Add(prm);
//
int n = mycmd.ExecuteNonQuery();
if (n > 0)
{
Class1.tt = new string[] { id, yqgl_da_add_yqid, zlbh, zlmc };
Class1.hasdo = "yes";
this.Close();
}
else
{
MessageBox.Show("添加失败!" + e.ToString());
}
5.读取图片
string sql = "select yqid,zlbh,zlmc,zltp from lab_yqgl_da where id='" + yqgl_da_change_id + "'";
SqlCommand cmd = new SqlCommand(sql, alluse.Class1.conn1);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "lab_yqgl_da");
int m = ds.Tables["lab_yqgl_da"].Rows.Count;
if (m > 0)
{
yqid = ds.Tables["lab_yqgl_da"].Rows[0]["yqid"].ToString();
this.comboBox1.Text = ds.Tables["lab_yqgl_da"].Rows[0]["zlbh"].ToString();
this.comboBox2.Text = ds.Tables["lab_yqgl_da"].Rows[0]["zlmc"].ToString();
//图片传到picturebox上。
Byte[] bytes = new Byte[0];
bytes = (Byte[])(ds.Tables["lab_yqgl_da"].Rows[m - 1]["zltp"]);
MemoryStream ms = new MemoryStream(bytes);
this.pictureBox1.Image = Image.FromStream(ms);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
//
}
缺陷
- 此测试不适用于 Access 和 SQL Server 中的罗斯文示例数据库的雇员表中的照片列。存储在照片列中的位图图像用由 Visual Basic 6.0 OLE Container 控件创建的标题信息进行了包装。
- 如果需要使用 Access 数据库测试此代码,则需要在 Access 表中创建一个 OLE Object 类型的列,并使用 Microsoft Jet 4.0 Provider 中的 System.Data.OleDb 名称空间代替 System.Data.SqlClient 名称空间。
- 关于图片上传数据库,下载到picturebox里的操作。
- C# 上传图片到数据库,下载到picturebox里。
- 存在数据库里的图片直接显示到PictureBox中
- 如何将pictureBox里的图片保存到数据库,然后从数据读取显示
- B6在Picturebox显示数据库里的二进制的图片
- 将图片上传到PictureBox控件中
- .net中关于PictureBox绑定数据库的图片字段
- net中关于PictureBox绑定数据库的图片字段 (转载)
- C# winform 另存为picturebox里的图片
- 关于图片保存到SQL数据库及从数据库读出显示在PictureBox中.
- c# winform 读取oracle中blob字段的图片显示到pictureBox里
- c#如何打印picturebox里的图片,winform怎样打印picturebox里的图片
- Delphi 将JPG图片上传到SqlServer数据库里
- Delphi 将JPG图片上传到SqlServer数据库里
- 读取本地图片到PICTUREBOX、以及和数据库指间的交互
- 郁闷了半天,原来是这个问题:关于C#中关于图片保存到数据库以及将数据库中的图片显示到PictureBox。
- 上传图片到数据库
- 上传图片到数据库
- c语言读取有特殊格式的txt
- 详解DIV+CSS与表格建站的区别
- 汉语
- 查询不同结构两表 字段数据不一样
- 用VB.NET捕获视频处理图像
- 关于图片上传数据库,下载到picturebox里的操作。
- DB2 V9.5的版本选择问题
- 浅谈数据库优化问题1
- [分享]sql server 2005/2008备份/删除过期备份T-sql 版本v1.0
- 破解微软KB905474正版验证补丁的方法
- java的部署工具ant详解
- 关于MSSql 2005 数据库的排序规则
- 介绍正规表达式
- 编程思想①群已满,现请加入②69119076