C#从SqlServer数据库读写文件源码
如下的资料是关于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数据库读写文件源码的更多相关文章
- Ocelot简易教程(七)之配置文件数据库存储插件源码解析
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9852711.html 上篇文章给大家分享了如何集成我写的一个Ocelot扩展插件把Ocelot的配置存储 ...
- vue.js使用webpack发布,部署到服务器上之后在浏览器中可以查看到vue文件源码
webpack+vue 2.0打包发布之后,将发布的文件部署到服务器中之后,浏览器中访问的时候会出现一个webpack文件夹,里边会显示vue文件源码 如果不想让vue源文件显示出来,可以在confi ...
- nodejs的tream(流)解析与模拟文件读写流源码实现
什么是流? 可读流于可写流 双工流于转换流 背压机制与文件流模拟实现 一.什么是流? 关于流的概念早在1964年就有记录被提出了,简单的说"流"就是控制数据传输过程的程序,比如在那 ...
- laravel 读写分离源码解析
前言:上一篇我们说了<laravel 配置MySQL读写分离>,这次我们说下,laravel的底层代码是怎样实现读写分离的. 一.实现原理 说明: 1.根据 database.php ...
- MySQL主从复制配置指导及PHP读写分离源码分析
开发环境 master环境:ubuntu16.04.5LTS/i5/8G/500G/64位/mysql5.7.23/php7/apache2 slave环境:kvm虚拟机/ubuntu14.04.01 ...
- lua连接数据库之luasql ------ luasql连接mysql数据库 及 luasql源码编译
lua连接数据库不只luasql这个库,但目前更新最快的的貌似是这个luasql,他是开源的,支持的数据库功能如下: Connect to ODBC, ADO, Oracle, MySQL, SQLi ...
- 一个通用的分页存储过程实现-SqlServer(附上sql源码,一键执行即刻搭建运行环境)
使用前提 查询表必须有ID字段,且该字段不能重复,建议为自增主键 背景 如果使用ADO.NET进行开发,在查询分页数据的时候一般都是使用分页存储过程来实现的,本文提供一种通用的分页存储过程,只需要传入 ...
- 开源分布式数据库中间件MyCat源码分析系列
MyCat是当下很火的开源分布式数据库中间件,特意花费了一些精力研究其实现方式与内部机制,在此针对某些较为重要的源码进行粗浅的分析,希望与感兴趣的朋友交流探讨. 本源码分析系列主要针对代码实现,配置. ...
- [转]MyEclipse 里查看jar文件源码
在开发过程中,有时候需要查看jar文件的源码,这里讲解如何设置. 选中某一个jar文件,如我这里选中的是struts2-core-2.1.6.jar,然后右键-->Properties--&g ...
随机推荐
- 手动编译安装nginx
1.下载nginx源码包并解压 可在http://nginx.org/en/download.html下载.tar.gz的源码包,如(nginx-1.4.7.tar.gz) 下载后通过tar -xvz ...
- [SQL]LeetCode178. 分数排名 | Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [Swift]LeetCode324. 摆动排序 II | Wiggle Sort II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [Swift]LeetCode679. 24点游戏 | 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- Jason Wang: 结对编程 CountWord(第三次作业)
本次作业地址: https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/2882 学号: 201731072323 ...
- java在方法中获取request对象
在spring的普通类中: HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getReques ...
- Linux清空文件内容
日志文件太多,需要清空: echo "" > mylog.log
- Java:多态乃幸福本源
01 多态是什么 在我刻板的印象里,西游记里的那段孙悟空和二郎神的精彩对战就能很好的解释“多态”这个词:一个孙悟空,能七十二变:一个二郎神,也能七十二变:他们都可以变成不同的形态,但只需要悄悄地喊一声 ...
- 使用reflector对.NET反编译
reflector的下载地址:https://www.cr173.com/soft/355285.html 反编译后的结果: