.Net中保存和读取数据库中Image类型(二进制)字段
2010-07-09 11:03
561 查看
保存入数据库 采用参数的方式传进去
SqlCommand myCommand =
new SqlCommand(sql, new SqlConnection(Configuration.ConnectionString));
sql = @"update *** set imgicc=@imgicc where Imgfno = '" + formNo + "' and Imgwno = '" + workNo + "'";
FileInfo iccinfo = new FileInfo(hidden_imgicc.Value);
myCommand.Parameters.Add("@imgicc", SqlDbType.Image, (int)iccinfo.Length);
byte[] contenticc = new byte[iccinfo.Length];
FileStream stream = iccinfo.OpenRead();
stream.Read(contenticc, 0, contenticc.Length);
stream.Close();
myCommand.Parameters["@imgicc"].Value = contenticc;
myCommand.CommandText = sql;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
读取到页面 使用WriteLocalImage方法
if (dsImg.Tables[0].Rows[0]["imgiccPath"] != null)
{
WriteLocalImage(dsImg.Tables[0].Rows[0]["imgiccPath"].ToString());
}
else
{
WriteNoImg();
}
private void WriteBinaryImage(byte[] img)
{
Response.Clear();
Response.ContentType = "image/jpeg";
try
{
Response.BinaryWrite(img);
}
catch
{
WriteNoImg();
}
}
private void WriteLocalImage(string path)
{
if (path.Trim() != string.Empty)
{
Response.Clear();
Response.ContentType = "image/jpeg";
Response.TransmitFile(path);
}
else
{
WriteNoImg();
}
}
private void WriteNoImg()
{
Response.Write("Image is not exists.");
}
SqlCommand myCommand =
new SqlCommand(sql, new SqlConnection(Configuration.ConnectionString));
sql = @"update *** set imgicc=@imgicc where Imgfno = '" + formNo + "' and Imgwno = '" + workNo + "'";
FileInfo iccinfo = new FileInfo(hidden_imgicc.Value);
myCommand.Parameters.Add("@imgicc", SqlDbType.Image, (int)iccinfo.Length);
byte[] contenticc = new byte[iccinfo.Length];
FileStream stream = iccinfo.OpenRead();
stream.Read(contenticc, 0, contenticc.Length);
stream.Close();
myCommand.Parameters["@imgicc"].Value = contenticc;
myCommand.CommandText = sql;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
读取到页面 使用WriteLocalImage方法
if (dsImg.Tables[0].Rows[0]["imgiccPath"] != null)
{
WriteLocalImage(dsImg.Tables[0].Rows[0]["imgiccPath"].ToString());
}
else
{
WriteNoImg();
}
private void WriteBinaryImage(byte[] img)
{
Response.Clear();
Response.ContentType = "image/jpeg";
try
{
Response.BinaryWrite(img);
}
catch
{
WriteNoImg();
}
}
private void WriteLocalImage(string path)
{
if (path.Trim() != string.Empty)
{
Response.Clear();
Response.ContentType = "image/jpeg";
Response.TransmitFile(path);
}
else
{
WriteNoImg();
}
}
private void WriteNoImg()
{
Response.Write("Image is not exists.");
}
相关文章推荐
- 谈谈有关各数据库Blob、Clob类型字段的读取、保存更新
- 如何将图片以Image类型存储到数据库 (asp.net) 如何从数据库中读取Image类型的字段(asp.net)
- .net 文件上传至数据库二进制字段 及读取操作。
- SqlDataSource读取数据库的Image类型字段
- 如何将图片以Image类型存储到数据库 如何从数据库中读取Image类型的字段
- php 读取sqlserver 2008 image类型(二进制)字段被截断问题(mssql.textsize)
- asp.net 从数据库中读取而二进制数据图片,展示在Image中
- 用vc读取数据库的字段值,保存在数组里
- mybatis oracle BLOB类型字段保存与读取
- 将数据库中以image类型保存的图片另存为jpg文件,并将其路径存入数据库
- 将文件读取成二进制数组保存到数据库
- c/vc/c++ 将mysql二进制字段(longblob类型)读出来保存成文件
- java读取数据库char类型字段问题
- 读取一个二进制文件保存到数据库中
- 数据库用一个int类型的字段保存前端多个字段(该字段为多选)
- 采用struts2+hibernate实现保存文件到Oracle数据库&&并附下载代码【导入即可用】;数据库中保存blob类型的二进制文件
- 【转帖】通过PHP读取dbf数据(visual fox pro,VFP数据库),官方的dbase无法读取字段为类型memo的数据,国外网站的解决方案 How to read FoxPro Memo with PHP?
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- 上传下载保存到数据库的image字段中的文件
- 图片保存到数据库及读取到image显示