[C#] C#从SqlServer数据库读写文件代码 →→→→→进入此内容的聊天室

来自 , 2019-05-19, 写在 C#, 查看 100 次.
URL http://www.code666.cn/view/d4d8d1ac
  1. <%@ Page Language="C#" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5.   private string connectionString = "Data Source=192.168.3.1;Initial Catalog=TestData;User Id=sa;Password=lambada;";
  6.   protected void Button1_Click(object sender, EventArgs e)
  7.   {
  8.   //得到文件数组
  9.   byte[] fileData = FileUpload1.FileBytes;
  10.   //得到文件名字
  11.   string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
  12.   //得到文件类型
  13.   string fileType = FileUpload1.PostedFile.ContentType;
  14.  
  15.   //构建数据库连接,SQL语句,创建参数
  16.    
  17.   System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
  18.   String strSql = "INSERT INTO FileTable (ContentType,Content,Title)" +
  19.   "VALUES (@ContentType,@Content,@Title)";
  20.   System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
  21.   command.Parameters.AddWithValue("@ContentType", fileType);
  22.   command.Parameters.AddWithValue("@Content", fileData);
  23.   command.Parameters.AddWithValue("@Title", fileName);
  24.   //打开连接,执行查询
  25.   myConnection.Open();
  26.   command.ExecuteNonQuery();
  27.   myConnection.Close();
  28.   myConnection.Dispose();
  29.   Response.Redirect(Request.FilePath);
  30.   }
  31.  
  32.   protected void Page_Load(object sender, EventArgs e)
  33.   {
  34.   /*
  35.    CREATE TABLE [FileTable] (
  36.     [FileId] [int] IDENTITY (1, 1) NOT NULL ,
  37.     [Title] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL ,
  38.     [ContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
  39.     [Content] [image] NULL ,
  40.     CONSTRAINT [PK_FileTable] PRIMARY KEY  CLUSTERED
  41.     (
  42.         [FileId]
  43.     )  ON [PRIMARY]
  44.   ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  45.  
  46.   */
  47.    
  48.   //构建数据库连接,SQL语句,创建参数
  49.   System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
  50.   String strSql = "select * from FileTable";
  51.   System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
  52.   //打开连接,执行查询
  53.   myConnection.Open();
  54.   System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
  55.   GridView1.DataSource = dr;
  56.   GridView1.DataBind();
  57.   myConnection.Close();
  58.   myConnection.Dispose();
  59.   }
  60. </script>
  61. <html xmlns="http://www.w3.org/1999/xhtml">
  62. <head id="Head1" runat="server">
  63.   <title></title>
  64. </head>
  65. <body>
  66.   <form id="form1" runat="server">
  67.   <asp:FileUpload ID="FileUpload1" runat="server" />
  68.   <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" />
  69.   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  70.   <Columns>
  71.   <asp:HyperLinkField DataNavigateUrlFields="FileId" HeaderText="文件名" DataTextField="Title" DataNavigateUrlFormatString="~/Download.ashx?FileId={0}" />
  72.   </Columns>
  73.   </asp:GridView>
  74.   </form>
  75. </body>
  76. </html>
  77.  
  78.  
  79. //csharp/6768

回复 "C#从SqlServer数据库读写文件代码"

这儿你可以回复上面这条便签

captcha