当前位置: 移动技术网 > IT编程>开发语言>.net > C#从SqlServer数据库读写文件源码

C#从SqlServer数据库读写文件源码

2019年01月12日  | 移动技术网IT编程  | 我要评论

gummy 雪花,张百发式下场,潜江个人二手房网

如下的资料是关于c#从sqlserver数据库读写文件的内容,希望能对码农们有一些用。

<%@ page language="c#" %>

<script runat="server">
private string connectionstring = "data source=192.168.3.1;initial catalog=testdata;user id=sa;password=lambada;";
protected void button1_click(object sender, eventargs e)
{
byte[] filedata = fileupload1.filebytes;
string filename = system.io.path.getfilename(fileupload1.filename);
string filetype = fileupload1.postedfile.contenttype;


system.data.sqlclient.sqlconnection myconnection = new system.data.sqlclient.sqlconnection(connectionstring);
string strsql = "insert into filetable (contenttype,content,title)" +
"values (@contenttype,@content,@title)";
system.data.sqlclient.sqlcommand command = new system.data.sqlclient.sqlcommand(strsql, myconnection);
command.parameters.addwithvalue("@contenttype", filetype);
command.parameters.addwithvalue("@content", filedata);
command.parameters.addwithvalue("@title", filename);
myconnection.open();
command.executenonquery();
myconnection.close();
myconnection.dispose();
response.redirect(request.filepath);
}

protected void page_load(object sender, eventargs e)
{
create table [filetable] (
[fileid] [int] identity (1, 1) not null ,
[title] [nvarchar] (255) collate chinese_prc_ci_as not null ,
[contenttype] [varchar] (50) collate chinese_prc_ci_as not null ,
[content] [image] null ,
constraint [pk_filetable] primary key clustered
(
[fileid]
) on [primary]
) on [primary] textimage_on [primary]


system.data.sqlclient.sqlconnection myconnection = new system.data.sqlclient.sqlconnection(connectionstring);
system.data.sqlclient.sqlcommand command = new system.data.sqlclient.sqlcommand(strsql, myconnection);
myconnection.open();
system.data.sqlclient.sqldatareader dr = command.executereader();
gridview1.datasource = dr;
gridview1.databind();
myconnection.close();
myconnection.dispose();
}
</script>
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:fileupload id="fileupload1" runat="server" />
<asp:button id="button1" runat="server" onclick="button1_click" text="上传文件" />
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false">
<columns>
<asp:hyperlinkfield datanavigateurlfields="fileid" headertext="文件名" datatextfield="title" datanavigateurlformatstring="~/download.ashx?fileid={0}" />
</columns>
</asp:gridview>
</form>
</body>
</html>






<%@ webhandler language="c#" class="download" %>
using system;
using system.web;
using system.data.sqlclient;
public class download : ihttphandler
{
private string connectionstring = "data source=192.168.3.1;initial catalog=testdata;user id=sa;password=lambada;";
public void processrequest(httpcontext context)
{
string fileid = context.request.querystring["fileid"];
if (string.isnullorempty(fileid))
{
context.response.contenttype = "text/html";
context.response.write("无效的id。");
return;
}
int id = 0;
if (!int32.tryparse(fileid, out id))
{
context.response.contenttype = "text/html";
context.response.write("id 不是数字。");
return;
}

sqlconnection myconnection = new sqlconnection(connectionstring);
sqlcommand command = new sqlcommand(strsql, myconnection);
command.parameters.addwithvalue("@fileid", fileid);
myconnection.open();
sqldatareader dr = command.executereader();
if (dr.read())
{
string filename = dr["title"].tostring();
if (context.request.useragent.indexof("msie") > -1)
{
filename = httputility.urlencode(filename);
}
context.response.clearcontent();
context.response.contenttype = dr["contenttype"].tostring();
context.response.addheader("content-disposition", "attachment;filename=" + filename);
context.response.binarywrite((byte[])dr["content"]);
}
else
{
context.response.contenttype = "text/html";
context.response.write("没找到文件。");
}
myconnection.close();
myconnection.dispose();
}

public bool isreusable
{
get
{
return false;
}
}

}





如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网