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

时间:2023-01-25 15:17:04

如下的资料是关于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;
}
}

}