
曾经写过一篇《如何上传文件(图片)》,文章里上传的文件或图片,是保存到服务器的硬盘里的。但是有朋友问,那么如何把图片上传到数据库中呢?
其实上传图片和上传文件到数据库中都是一样的。我们先新建一个access数据库,命名为img.mdb。然后在img.mdb数据库里建一个表,表名为tbImg。为了简单起见,这个表只包含两个字段,一个是ID,自动编号类型的;另一个字段名为:imgdata,是OLE 对象型的。
在OLE 对象型的字段里,我们可以存入任何二进制的内容。包括图像、文件、声音等等。
现在在项目里建立一个img2access.aspx文件,在文件里添加一个File Field控件和一个按钮。
然后进入HTML代码,把“<INPUT type="file">”改成“<INPUT type="file" runat="server" id="myFile">”
再回到img2access.aspx页面,双击按钮,在img2access.aspx.cs页面里,先添加using System.Data.OleDb;和using System.IO;的引用。再在Page_Load事件里加上以下代码:
if (!this.IsPostBack)
{}
然后在Button1_Click事件里加入以下代码:
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath("img.mdb"));
Stream FileStream=this.myFile.PostedFile.InputStream;
int FileLen=this.myFile.PostedFile.ContentLength;
byte[] FileData=new byte[FileLen];
int n = FileStream.Read(FileData,0,FileLen);
string strSql = "INSERT INTO tbImg (imgdata) VALUES (@imgdata)";
myConn.Open();
OleDbCommand myCommand = new OleDbCommand(strSql,myConn);
myCommand.Parameters.Add("@imgdata",OleDbType.Binary);
myCommand.Parameters["@imgdata"].Value = FileData;
int iout = myCommand.ExecuteNonQuery();
myConn.Close();
if (iout==1)
{
Response.Write("添加成功");
}
编译后就可以上传图片、文件等进入数据库中了。完整代码如下:
img2access.aspx:
<%@ Page language="c#" Codebehind="img2access.aspx.cs" AutoEventWireup="false" Inherits="test.img2access" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>img2access</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><INPUT type="file" runat="server" id="myFile"></FONT>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
img2access.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
namespace test
{
/// <summary>
/// img2access 的摘要说明。
/// </summary>
public class img2access : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath("img.mdb"));
Stream FileStream=this.myFile.PostedFile.InputStream; //建立一个Stream对象
int FileLen=this.myFile.PostedFile.ContentLength; //图片数据的字节大小
byte[] FileData=new byte[FileLen];
int n = FileStream.Read(FileData,0,FileLen);
string strSql = "INSERT INTO tbImg (imgdata) VALUES (@imgdata)";
myConn.Open();
OleDbCommand myCommand = new OleDbCommand(strSql,myConn);
myCommand.Parameters.Add("@imgdata",OleDbType.Binary);
myCommand.Parameters["@imgdata"].Value = FileData;
int iout = myCommand.ExecuteNonQuery();
myConn.Close();
if (iout==1)
{
Response.Write("添加成功");
}
}
}
}
: 科技


