图片以二进制的方式存入数据库,以及取出图片
2012-07-20 14:51
357 查看
protected void saveimage_Click(object sender, EventArgs e)
{
#region 把图片以二进制的形式存入数据库
Stream imageStream;
string path = fileupload.PostedFile.FileName;//文件名
int size = fileupload.PostedFile.ContentLength;//文件大小
string type = fileupload.PostedFile.ContentType;//文件类型
imageStream = fileupload.PostedFile.InputStream;
byte[] content = new byte[size];
//=Request.QueryString["name"];
string name = this.name.Text.Trim();
int status = imageStream.Read(content, 0, size);
string sql = "insert into pacture (image,name)values(@image,@name) ";
SqlParameter[] pams = {
new SqlParameter("@image",content),
new SqlParameter("@name",name)
};
int i = ExecuteNonQuery(sql, pams);
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), null, "alert('成功!')", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), null, "alert('失败!')", true);
}
#endregion
}
此方法中:image字段的数据类型应为image
protected void getimage_Click(object sender, EventArgs e)
{
string sql = "select * from pacture";
DataTable dt = ExecuteDataTable(sql, null);
byte[] bt=null;
if (dt.Rows.Count > 0)
{
bt = (byte[])dt.Rows[0][1];
}
#region 把图片输出到指定目录
FileStream fs = new FileStream(@"H:\1.jpg", FileMode.Create, FileAccess.Write);
fs.Write(bt, 0, bt.Length);
fs.Flush();
fs.Close();
#endregion
#region 把图片显示在网页
//Response.ContentType = "application/octet-stream";
//Response.BinaryWrite(bt);
//Response.End();
#endregion
}
{
#region 把图片以二进制的形式存入数据库
Stream imageStream;
string path = fileupload.PostedFile.FileName;//文件名
int size = fileupload.PostedFile.ContentLength;//文件大小
string type = fileupload.PostedFile.ContentType;//文件类型
imageStream = fileupload.PostedFile.InputStream;
byte[] content = new byte[size];
//=Request.QueryString["name"];
string name = this.name.Text.Trim();
int status = imageStream.Read(content, 0, size);
string sql = "insert into pacture (image,name)values(@image,@name) ";
SqlParameter[] pams = {
new SqlParameter("@image",content),
new SqlParameter("@name",name)
};
int i = ExecuteNonQuery(sql, pams);
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), null, "alert('成功!')", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), null, "alert('失败!')", true);
}
#endregion
}
此方法中:image字段的数据类型应为image
protected void getimage_Click(object sender, EventArgs e)
{
string sql = "select * from pacture";
DataTable dt = ExecuteDataTable(sql, null);
byte[] bt=null;
if (dt.Rows.Count > 0)
{
bt = (byte[])dt.Rows[0][1];
}
#region 把图片输出到指定目录
FileStream fs = new FileStream(@"H:\1.jpg", FileMode.Create, FileAccess.Write);
fs.Write(bt, 0, bt.Length);
fs.Flush();
fs.Close();
#endregion
#region 把图片显示在网页
//Response.ContentType = "application/octet-stream";
//Response.BinaryWrite(bt);
//Response.End();
#endregion
}
相关文章推荐
- WinForm把图片以二进制的方式存入到数据库,并将存入数据库的二进制图片读出显示到PictureBox中
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- ASP.Net将图片以二进制方式存入数据库,并读取
- ASP.Net将图片以二进制方式存入数据库,并读取
- 将图片以二进制数组存入数据库,从数据库中取出转为图片
- 将图片以二进制格式存入数据库,并以流的方式展现在jsp页面
- VB ADO 用二进制方式来读写文件 入数据库 __________请参考(把文件(包括图片)存入数据库)
- 利用数据库如何存入BLOB格式图片,并从数据库中取出BLBO格式图片显示出来
- ASP.NET实现图片以二进制的形式存入数据库
- 将图片以二进制的方式保存在数据库中,并显示图片
- 图片转二进制存入oracle字段类型为Blob(取出二进制转图片并显示),图片带路径
- ASP.NET实现图片以二进制的形式存入数据库
- 图片存入数据库并取出显示
- MyBatis框架(三)动态SQL,分页,二进制存入数据库图片
- C# 二进制数据、图片存入、读出 sql server 数据库
- 将图片以二进制形式存入数据库中
- C#把图片以二进制形式存入数据库
- PHP将二进制文件存入数据库以及从数据库中读取二进制文件
- net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件
- 常用的DBOperate之五——将图片以二进制存入数据库中