用ado.net对word,excel进行存取。 (blob方式)
2008-04-06 21:45
281 查看
blob表
3 id int 4 0
0 name char 50 1
0 blob image 16 1
0 type char 60 1
saveFile.aspx.cs
private
void Button1_Click(object sender, System.EventArgs e)
{
Stream
imgdatastream = File1.PostedFile.InputStream;
int
imgdatalen = File1.PostedFile.ContentLength;
string
imgtype = File1.PostedFile.ContentType;
string name =
this.getFileNameByURL(this.File1.PostedFile.FileName);
byte[]
imgdata = new byte[imgdatalen];
int n =
imgdatastream.Read(imgdata,0,imgdatalen);
string
connstr = "workstation id=OVERMIND;packet
size=4096;user id=sa;password=sa;data source=OVERMIND;persist
security info=False;initial catalog=wztj";
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand("INSERT INTO
blob(name,type,blob) VALUES ( @imgtitle,
@type,@blob )", connection );
SqlParameter
paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50
);
paramTitle.Value
= name;
command.Parameters.Add(paramTitle);
SqlParameter
paramData = new SqlParameter( "@blob", SqlDbType.Image );
paramData.Value
= imgdata;
command.Parameters.Add(
paramData );
SqlParameter
paramType = new SqlParameter( "@type", SqlDbType.VarChar,50
);
paramType.Value
= imgtype;
command.Parameters.Add(
paramType );
wztj.debug.TestSQL.TraceErrorSql("INSERT
INTO blob(name,type,blob) VALUES ( @imgtitle,
@type,@blob )",command.Parameters);
connection.Open();
int
numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
listFile.aspx//这个东西主要用来列表,把已经有的东西列出来
<asp:HyperLinkColumn DataNavigateUrlField="id"
HeaderText="产品名称"
DataNavigateUrlFormatString="./getFile.aspx?ID={0}" DataTextField="name"
DataTextFormatString="{0}" ItemStyle-HorizontalAlign="Center"
ItemStyle-Width="160px">
listFile.aspx.cs
string
connstr="workstation id=OVERMIND;packet size=4096;user
id=sa;password=sa;data source=OVERMIND;persist security
info=False;initial catalog=wztj";
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand("select * from blob", connection );
connection.Open();
SqlDataAdapter
adaptor = new SqlDataAdapter(command);
DataSet ds =
new DataSet();
adaptor.Fill(ds,"blob");
connection.Close();
this.DataGrid1.DataSource=ds.Tables["blob"].DefaultView;
this.DataGrid1.DataBind();
getFile.aspx.cs//这个文件比较重要负责把村道数据库里面的文件,按照格式,按照名称,给传输出来
private
void Page_Load(object sender, System.EventArgs e)
{
string imgid
=this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string
connstr="workstation id=OVERMIND;packet size=4096;user
id=sa;password=sa;data source=OVERMIND;persist security
info=False;initial catalog=wztj";
string
sql="SELECT name,blob, type FROM blob WHERE id
= " + imgid;
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader
dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.Buffer=
true;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType
= "application/ms-word";//设置输出文件类型为word文件。
Response.ContentType
= dr["type"].ToString();
Response.BinaryWrite(
(byte[]) dr["blob"] );
string
FileName = dr["name"].ToString().Trim();
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8
);
Response.AppendHeader("Content-Disposition",
"attachment;filename="+FileName);
}
connection.Close();
}
这里要说的有两点,第一,就是把文件的名称getFile.aspx变成我们想要的名称。
Response.AppendHeader("Content-Disposition",
"attachment;filename="+FileName);
第二,就是把指定的名称变成我们想要的值,是标准的中文,而不是中文的乱码。
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8
);
<
3 id int 4 0
0 name char 50 1
0 blob image 16 1
0 type char 60 1
saveFile.aspx.cs
private
void Button1_Click(object sender, System.EventArgs e)
{
Stream
imgdatastream = File1.PostedFile.InputStream;
int
imgdatalen = File1.PostedFile.ContentLength;
string
imgtype = File1.PostedFile.ContentType;
string name =
this.getFileNameByURL(this.File1.PostedFile.FileName);
byte[]
imgdata = new byte[imgdatalen];
int n =
imgdatastream.Read(imgdata,0,imgdatalen);
string
connstr = "workstation id=OVERMIND;packet
size=4096;user id=sa;password=sa;data source=OVERMIND;persist
security info=False;initial catalog=wztj";
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand("INSERT INTO
blob(name,type,blob) VALUES ( @imgtitle,
@type,@blob )", connection );
SqlParameter
paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50
);
paramTitle.Value
= name;
command.Parameters.Add(paramTitle);
SqlParameter
paramData = new SqlParameter( "@blob", SqlDbType.Image );
paramData.Value
= imgdata;
command.Parameters.Add(
paramData );
SqlParameter
paramType = new SqlParameter( "@type", SqlDbType.VarChar,50
);
paramType.Value
= imgtype;
command.Parameters.Add(
paramType );
wztj.debug.TestSQL.TraceErrorSql("INSERT
INTO blob(name,type,blob) VALUES ( @imgtitle,
@type,@blob )",command.Parameters);
connection.Open();
int
numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
listFile.aspx//这个东西主要用来列表,把已经有的东西列出来
<asp:HyperLinkColumn DataNavigateUrlField="id"
HeaderText="产品名称"
DataNavigateUrlFormatString="./getFile.aspx?ID={0}" DataTextField="name"
DataTextFormatString="{0}" ItemStyle-HorizontalAlign="Center"
ItemStyle-Width="160px">
listFile.aspx.cs
string
connstr="workstation id=OVERMIND;packet size=4096;user
id=sa;password=sa;data source=OVERMIND;persist security
info=False;initial catalog=wztj";
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand("select * from blob", connection );
connection.Open();
SqlDataAdapter
adaptor = new SqlDataAdapter(command);
DataSet ds =
new DataSet();
adaptor.Fill(ds,"blob");
connection.Close();
this.DataGrid1.DataSource=ds.Tables["blob"].DefaultView;
this.DataGrid1.DataBind();
getFile.aspx.cs//这个文件比较重要负责把村道数据库里面的文件,按照格式,按照名称,给传输出来
private
void Page_Load(object sender, System.EventArgs e)
{
string imgid
=this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string
connstr="workstation id=OVERMIND;packet size=4096;user
id=sa;password=sa;data source=OVERMIND;persist security
info=False;initial catalog=wztj";
string
sql="SELECT name,blob, type FROM blob WHERE id
= " + imgid;
SqlConnection
connection = new SqlConnection(connstr);
SqlCommand
command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader
dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.Buffer=
true;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType
= "application/ms-word";//设置输出文件类型为word文件。
Response.ContentType
= dr["type"].ToString();
Response.BinaryWrite(
(byte[]) dr["blob"] );
string
FileName = dr["name"].ToString().Trim();
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8
);
Response.AppendHeader("Content-Disposition",
"attachment;filename="+FileName);
}
connection.Close();
}
这里要说的有两点,第一,就是把文件的名称getFile.aspx变成我们想要的名称。
Response.AppendHeader("Content-Disposition",
"attachment;filename="+FileName);
第二,就是把指定的名称变成我们想要的值,是标准的中文,而不是中文的乱码。
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8
);
<
相关文章推荐
- 用ado.net对word,excel进行存取。 (blob方式)
- ADO.net执行存取过程时间过长的一个解决方式
- ADO.NET方式读取Excel 不完整问题
- 数据访问技术系列课程 笔记(5) ADO.NET 非连接方式进行数据访问
- ADO.NET方式读取Excel 不完整问题
- 采用ADO.NET存取Excel数据(Use ADO.NET to Retrieve and Modify Records in an Excel Workbook)
- 图解Ado.net连接数据库并存取数据的几种方式
- 图解Ado.net连接数据库并存取数据的几种方式
- ADO.NET 连接方式进行数据访问
- 数据访问技术系列课程 笔记(2) ADO.NET 连接方式进行数据访问
- ADO.NET 连接方式进行数据访问
- 利用ado.net存取BLOB数据
- ADO.NET 连接方式进行数据访问学习笔记
- ADO.NET 连接方式进行数据库访问
- asp.net Aspose.Word 利用邮件合并功能 操作word模板 生成word。asp.net NPOI 对excel进行操作,生成excel
- 为什么用ADO.NET方式读取Excel数字读不出来?
- c#.net 将网页内容转换成word或excel文档
- 利用ADO.NET连接Excel数据库,并执行相应的操作
- ADO.NET データベース接続方式