如下的资料是关于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]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  2. Nginx 动静分离与负载均衡的实现

    一.前提 企业中,随着用户的增长,数据量也几乎成几何增长,数据越来越大,随之也就出现了各种应用的瓶颈问题. 问题出现了,我们就得想办法解决,一般网站环境,均会使用LAMP或者LNMP,而我们对于网站环 ...

  3. C/C++数据在内存中的存储方式

    目录 1 内存地址 2 内存空间   在学习C/C++编程语言时,免不了和内存打交道,在计算机中,我们存储有电影,文档,音乐等数据,这些数据在内存中是以什么形式存储的呢?下面做一下简单介绍. 本文是学 ...

  4. Java高级开发工程师面试笔记

    最近在复习面试相关的知识点,然后做笔记,后期(大概在2018.02.01)会分享给大家,尽自己最大的努力做到最好,还希望到时候大家能给予建议和补充 ----------------2018.03.05 ...

  5. nginx替换响应头(重点:如何在替换时加上if判断)

    在实现微信小程序内嵌非业务域名时,通过nginx做镜像网站绕过小程序业务域名检测,但有一些表单页面提交后会返回一个302状态,由响应头Location的值决定提交成功后的跳转地址.那么问题来了,这个地 ...

  6. gradle插件从3.2.0升级到3.2.1后报错Error: Cannot create directory 项目目录\thirdlib\build\intermediates\packaged_res\debug\drawable

    报错信息如下:  解决方案: 删除thirdlib\build目录,然后重新编译. 但是紧接着又会报类似的错误,只不过build目录变成其他module的了. 所以,先clear build,然后再重 ...

  7. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

    在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...

  8. EF架构~TransactionScope与SaveChanges的关系

    回到目录 TransactionScope是.net环境下的事务,可以提升为分布式事务,这些知识早在很久前就已经说过了,今天不再说它,今天主要谈谈Savechanges()这个方法在Transacti ...

  9. shell实战之tomcat看门狗

    1.脚本简介 tomcat看门狗,在tomcat进程异常退出时会自动拉起tomcat进程并记录tomcat运行的日志. 函数说明: log_info:打印日志的函数,入参为需要在日志中打印的msg s ...

  10. Asp.Net SignalR 多平台的Client与Server

    多平台 SignalR在.Net的大环境下都可以做到即时通讯,也就是说都可以使用,客户端也不仅是js.下面就来一个控制台的Client 我们需要在nuget上下载包 Microsoft.AspNet. ...