存取显示Image类型的字段
2008-11-26 13:46
411 查看
using System;using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
Int32 FileLength = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
//存储
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength;
try
{
if (FileLength == 0)
{
lblMessage.Text = "<b>请选择您要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObj = UpFile.InputStream;//建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObj.Read(FileByteArray, 0, FileLength);
SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
lblMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
//读取
private void ImgDataRead()
{
int ImgID = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));
Response.BufferOutput = true;
Con.Close();
}
}
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
Int32 FileLength = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
//存储
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength;
try
{
if (FileLength == 0)
{
lblMessage.Text = "<b>请选择您要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObj = UpFile.InputStream;//建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObj.Read(FileByteArray, 0, FileLength);
SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
lblMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
//读取
private void ImgDataRead()
{
int ImgID = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));
Response.BufferOutput = true;
Con.Close();
}
}
相关文章推荐
- ADO 访问数据库 存取image类型字段数据 源代码
- 使用asp.net和C#如何存取Sql Server2000中的Image(16)类型的字段?
- (转载)VB 查询Oracle中blob类型字段,并且把blob中的图片以流的方式显示在Image上
- laravel按用户类型来显示或隐藏字段
- 如何显示数据库中Image类型的图片
- Sql Server 中image类型迁移到Oracle 中Blob类型出现图片显示不出来,why????请博客们帮忙
- 怎样把SQL_SERVER数据库里的(类型是image)图片显示在aspx页面里的image控件里
- VFP将图片或其他文件保存到SQL Server的Text类型字段或者Access的备注字段中,并可以还原或显示
- 如何将图片以Image类型存储到数据库 (asp.net) 如何从数据库中读取Image类型的字段(asp.net)
- boolean类型的字段值在DataGrid里显示值为"-1"与"0",怎么样才能让他显示:Ture或false
- Table 组件使用指南之四:使用CheckBox组件保存和显示Boolean类型字段
- visio2003 数据表模型中显示字段类型和注释
- 解决SQL Server的TEXT、IMAGE类型字段的长度限制
- 对于sqlalchemy——text类型字段存取json一体化的代码
- source_insight3.5字体全部变成了黑色,不能对不同类型的字段显示不同的颜色,如何解决?
- gridview中设置绑定的日期时间类型字段的显示格式
- 例程:asp.net中将大文件保存到MSSQL的image字段并实时显示进度信息
- SqlDataSource读取数据库的Image类型字段
- VFP将图片或其他文件保存到SQL Server的Text类型字段或者Access的备注字段中,并可以还原或显示。
- 从sql中image类型字段中导出图片