您的位置:首页 > 数据库

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 和 SQL Server 中的罗斯文示例数据库的雇员表中的照片列。存储在照片列中的位图图像用由 Visual Basic 6.0 OLE Container 控件创建的标题信息进行了包装。

如果需要使用 Access 数据库测试此代码,则需要在 Access 表中创建一个 OLE Object 类型的列,并使用 Microsoft Jet 4.0 Provider 中的 System.Data.OleDb 名称空间代替 System.Data.SqlClient 名称空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: