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

}

C#从SqlServer数据库读写文件源码的更多相关文章

  1. Ocelot简易教程(七)之配置文件数据库存储插件源码解析

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9852711.html 上篇文章给大家分享了如何集成我写的一个Ocelot扩展插件把Ocelot的配置存储 ...

  2. vue.js使用webpack发布,部署到服务器上之后在浏览器中可以查看到vue文件源码

    webpack+vue 2.0打包发布之后,将发布的文件部署到服务器中之后,浏览器中访问的时候会出现一个webpack文件夹,里边会显示vue文件源码 如果不想让vue源文件显示出来,可以在confi ...

  3. nodejs的tream(流)解析与模拟文件读写流源码实现

    什么是流? 可读流于可写流 双工流于转换流 背压机制与文件流模拟实现 一.什么是流? 关于流的概念早在1964年就有记录被提出了,简单的说"流"就是控制数据传输过程的程序,比如在那 ...

  4. laravel 读写分离源码解析

    前言:上一篇我们说了<laravel 配置MySQL读写分离>,这次我们说下,laravel的底层代码是怎样实现读写分离的.   一.实现原理 说明: 1.根据 database.php ...

  5. MySQL主从复制配置指导及PHP读写分离源码分析

    开发环境 master环境:ubuntu16.04.5LTS/i5/8G/500G/64位/mysql5.7.23/php7/apache2 slave环境:kvm虚拟机/ubuntu14.04.01 ...

  6. lua连接数据库之luasql ------ luasql连接mysql数据库 及 luasql源码编译

    lua连接数据库不只luasql这个库,但目前更新最快的的貌似是这个luasql,他是开源的,支持的数据库功能如下: Connect to ODBC, ADO, Oracle, MySQL, SQLi ...

  7. 一个通用的分页存储过程实现-SqlServer(附上sql源码,一键执行即刻搭建运行环境)

    使用前提 查询表必须有ID字段,且该字段不能重复,建议为自增主键 背景 如果使用ADO.NET进行开发,在查询分页数据的时候一般都是使用分页存储过程来实现的,本文提供一种通用的分页存储过程,只需要传入 ...

  8. 开源分布式数据库中间件MyCat源码分析系列

    MyCat是当下很火的开源分布式数据库中间件,特意花费了一些精力研究其实现方式与内部机制,在此针对某些较为重要的源码进行粗浅的分析,希望与感兴趣的朋友交流探讨. 本源码分析系列主要针对代码实现,配置. ...

  9. [转]MyEclipse 里查看jar文件源码

    在开发过程中,有时候需要查看jar文件的源码,这里讲解如何设置.  选中某一个jar文件,如我这里选中的是struts2-core-2.1.6.jar,然后右键-->Properties--&g ...

随机推荐

  1. [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  2. [Swift]LeetCode732. 我的日程安排表 III | My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  3. [Swift]LeetCode878. 第 N 个神奇数字 | Nth Magical Number

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  4. Android开发:Android虚拟机启动错误Can't find 'Linux version ' string in kernel image file

    Android启动出错,虚拟机报错信息如下: Starting emulator for AVD 'test' emulator: ERROR: Can't find 'Linux version ' ...

  5. React 中 Link 和 NavLink 组件 activeClassName、activeStyle 属性不生效的问题

    首先 导航链接应该使用  NavLink 而不再是  Link NavLink 使用方法见 https://github.com/ReactTraining/react-router/blob/mas ...

  6. CentOS 7 源码编译安装 Nginx

    这里安装的是nginx 1.14版本 1.下载源码 #下载 wget http://nginx.org/download/nginx-1.14.0.tar.gz #解压 tar -xzf nginx- ...

  7. 『ice 离散化广搜』

    ice(USACO) Description Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000. 湖面上的N(1 & ...

  8. SpringBoot入门教程(十)应用监控Actuator

    Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能.spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来 ...

  9. 《HelloGitHub月刊》第 09 期

    <HelloGitHub>第 09 期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 前言 转眼就到年底了,月刊做到了第09期,感谢大家一路的支持和帮助

  10. Spring Boot 2.x (一):HelloWorld

    简介 本系列基于Spring Boot 2.1.0 的官方文档,除去了文档中一些冗余的东西,加上了一些自己的理解,意图是在于帮助更多初识Spring Boot的人来进行一次探险. 本系列建议具有Jav ...