C# 上传图片到数据库,下载到picturebox里。
2009-04-13 12:06
429 查看
1.首先创建数据
id yqid zlbh zlmc zltp(资料图片img格式)
lab_yqgl_da
我的数据库及字段。
2.在窗体的代码模块顶部插入 using 语句:(操作图片后面添加,请查看!)
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
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上。
if (ds.Tables["lab_yqgl_da"].Rows[0]["zltp"] == System.DBNull.Value)
{
this.pictureBox1.Image = null;
}
else
{
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 数据库测试此代码,则需要在 Access 表中创建一个 OLE Object 类型的列,并使用 Microsoft Jet 4.0 Provider 中的 System.Data.OleDb 名称空间代替 System.Data.SqlClient 名称空间。
id yqid zlbh zlmc zltp(资料图片img格式)
lab_yqgl_da
我的数据库及字段。
2.在窗体的代码模块顶部插入 using 语句:(操作图片后面添加,请查看!)
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
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上。
if (ds.Tables["lab_yqgl_da"].Rows[0]["zltp"] == System.DBNull.Value)
{
this.pictureBox1.Image = null;
}
else
{
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里的操作。
- Java+mysql本地图片上传数据库及下载示例
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
- 图片上传到数据库与显示(转载,C#→VB)
- Java+mysql本地图片上传数据库及下载示例
- C#上传byte类型图片到数据库
- [上传下载] C# ImageUpload图片上传类教程与源码下载 (转载)
- 上传图片到数据库,读取数据库中图片并显示(C#)
- ASP.NET C#(4.0) + SQL2008 R2 多图片上传并生成缩略图一并保存至数据库
- 转:上传图片到数据库,读取数据库中图片并显示(C#)
- C#将文件上传、下载(以二进制流保存到数据库)
- C#通过简单的Http服务上传下载图片。
- 关于C#服务器控件FileUpload上传图片并保存到数据库
- 郁闷了半天,原来是这个问题:关于C#中关于图片保存到数据库以及将数据库中的图片显示到PictureBox。
- 将上传图片打上防伪图片水印并写入数据库ASP.NET C#
- VS 2010中WinForm开发(C#)—图片上传到数据库与显示(sql server 2008)
- C#将数据库图片显示在pictureBox
- C# 单文件与二进制互转数据库写入读出(上传文件二进制方式到DB中,下载DB中的二进制文件到本地)
- Flex上传图片等信息到数据库,并下载显示图片
- rest上传图片和下载图片(c#和c++)