当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET2.0中将文件上传到数据库

ASP.NET
Active Directory 对象的控制
在vb中动态加载水晶报表rpt文件的方法
webconfig中进行登陆的权限、修改2
DataGrid(WinForm)显示行号最简单的方法
在EXCEL中获取列中不重复的值的个数
使用Visual C#制作可伸缩个性化窗体
关于控件注册和使用许可问题的解决办法
获取闭合符号中的字符串
判断点与多边形的状态(位置)
NHibernate初试
GetTickCount()函数精确到多少毫秒
VB.net中HOOK的应用(一)
C# to VB.Net translator..
用Visual C#调用Windows API函数
用API函数实现切换VCD的左右声道
元数据--自定义属性(VB.NET)
初学VB.NET连接SQL数据库
简单的DataGrid多表头制作方法
带输出参数的存储过程的使用及在C#中调用问题
VS的控件真是不好用,好不容易才搞定DataGrid

ASP.NET2.0中将文件上传到数据库


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 143 ::
收藏到网摘: n/a

此问题经常被人问,本文列出将文字和图片上传到数据库的方法。包括Access数据库和SQL Server数据库。

Access数据库代码

<%@ Page Language="C#" EnableViewState="true" %>

<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Button1_Click( object sender, EventArgs e )
  {
    System.IO.Stream fileDataStream = FileUpload1.PostedFile.InputStream;

    if (fileDataStream.Length < 1)
    {
      Msg.Text = "请选择文件。";
      return;
    }

    //得到文件大小
    int fileLength = FileUpload1.PostedFile.ContentLength;

    //创建数组
    byte[] fileData = new byte[fileLength];
    //把文件流填充到数组
    fileDataStream.Read(fileData, 0, fileLength);
    //得到文件类型
    string fileType = FileUpload1.PostedFile.ContentType;

    //构建数据库连接,SQL语句,创建参数
    string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand command = new OleDbCommand("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
    "VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);

    command.Parameters.AddWithValue("@PersonName",TextBox1.Text);
    command.Parameters.AddWithValue("@PersonEmail", "[email protected]");
    command.Parameters.AddWithValue("@paramPersonSex", "男");
    command.Parameters.AddWithValue("@PersonImageType", fileType);
    command.Parameters.AddWithValue("@PersonImage", fileData);


    //打开连接,执行查询
    myConnection.Open();
    command.ExecuteNonQuery();
    myConnection.Close();
    Response.Redirect(Request.RawUrl);
  }


  protected void Page_Load( object sender, EventArgs e )
  {

    if (!Page.IsPostBack)
    {
      BindGrid();
    }
  }

  private void BindGrid( )
  {
    string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand myCommand = new OleDbCommand("SELECT * FROM Person", myConnection);

    try
    {
      myConnection.Open();
      GridView1.DataSource = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
      GridView1.DataBind();
    }
    catch (OleDbException SQLexc)
    {
      Response.Write("提取数据时出现错误:" + SQLexc.ToString());
    }
  }
  protected string FormatURL( object strArgument )
  {
    return "ReadImage.aspx?id=" + strArgument.ToString();
  } 

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>上传文件到数据库</title>
</head>
<body>
  <form id="MengXianhui" runat="server">
&nbs