将文字内容和图片插入到Access数据库(转帖)

时间:2006/5/24 18:49:41      阅读:3862          

从别人网站中转过来的呵,还不错

Image2Access.aspx

 1<%@ Page language="c#" Debug="true" Codebehind="Image2Access.aspx.cs"
 2AutoEventWireup="false" Inherits="eMeng.Exam.Image2Access" %>
 3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 4<HTML>
 5<HEAD>
 6<title>上传文件到 Access 数据库</title>
 7<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
 8<meta name="CODE_LANGUAGE" Content="C#">
 9<meta name="vs_defaultClientScript" content="JavaScript">
10<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
11</HEAD>
12<body MS_POSITIONING="GridLayout">
13<form id="DataGridShowImage" method="post" runat="server" enctype="multipart/form-data">
14<h3 align="center">上传文件到 Access 数据库</h3>
15<asp:DataGrid ID="DG_Persons" AutoGenerateColumns="False" Width="99%" HeaderStyle-BackColor="#ff0000"
16HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige" BorderColor="#000000"
17Runat="server" HeaderStyle-HorizontalAlign="Center">
18<Columns>
19    <asp:TemplateColumn HeaderText="姓名">
20        <ItemTemplate>
21            <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/>
22        </ItemTemplate>
23    </asp:TemplateColumn>
24    <asp:TemplateColumn HeaderText="电子邮件">
25        <ItemTemplate>
26            <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/>
27        </ItemTemplate>
28    </asp:TemplateColumn>
29    <asp:TemplateColumn HeaderText="性别">
30        <ItemTemplate>
31            <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/>
32        </ItemTemplate>
33    </asp:TemplateColumn>
34    <asp:TemplateColumn HeaderText="照片">
35        <ItemTemplate>
36            <asp:Image Runat=server ID="Image1" 
37            ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>' />
38        </ItemTemplate>
39    </asp:TemplateColumn>
40</Columns>
41</asp:DataGrid>
42<b>文件名字:</b><input id="MyFileName" type="text" runat="server" NAME="MyFileName">
43<P>
44<b>文件:</b><input id="MyFile" type="file" runat="server" NAME="MyFile">
45<br>
46<br>
47<input type="submit" value="开始上传" runat="server" ID="Submit1" NAME="Submit1">
48</P>
49</form>
50</body>
51</HTML>
52
53

Image2Access.aspx.cs

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Data.OleDb;
  6using System.Drawing;
  7using System.Web;
  8using System.IO;
  9using System.Web.SessionState;
 10using System.Web.UI;
 11using System.Web.UI.WebControls;
 12using System.Web.UI.HtmlControls;
 13
 14namespace eMeng.Exam
 15{
 16/// <summary>
 17/// Image2Access 的摘要说明。
 18/// </summary>
 19public class Image2Access : System.Web.UI.Page
 20{
 21protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
 22protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
 23protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
 24protected System.Web.UI.WebControls.DataGrid DG_Persons;
 25
 26private void Page_Load(object sender, System.EventArgs e)
 27{
 28// 在此处放置用户代码以初始化页面
 29BindGrid();
 30}
 31private void BindGrid()
 32{
 33string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
 34+ Server.MapPath("Image2Access.mdb");
 35OleDbConnection myConnection = new OleDbConnection(strCnn);
 36OleDbCommand myCommand = new OleDbCommand("SELECT * FROM Person", myConnection);
 37myCommand.CommandType = CommandType.Text;
 38try
 39{
 40myConnection.Open();
 41DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
 42DG_Persons.DataBind();
 43}
 44catch(OleDbException SQLexc)
 45{
 46Response.Write("提取数据时出现错误:" + SQLexc.ToString());
 47}
 48}
 49protected string FormatURL(object strArgument)
 50{
 51return "ReadImage.aspx?id=" + strArgument.ToString();
 52}
 53
 54Web 窗体设计器生成的代码
 75
 76private void Submit1_ServerClick(object sender, System.EventArgs e)
 77{
 78//得到提交的文件
 79Stream fileDataStream = MyFile.PostedFile.InputStream;
 80
 81//得到文件大小
 82int fileLength = MyFile.PostedFile.ContentLength;
 83
 84//创建数组
 85byte[] fileData = new byte[fileLength];
 86
 87//把文件流填充到数组
 88fileDataStream.Read(fileData,0,fileLength);
 89
 90//得到文件名字
 91string fileTitle = MyFileName.Value;
 92
 93//得到文件类型
 94string fileType = MyFile.PostedFile.ContentType;
 95
 96//构建数据库连接,SQL语句,创建参数
 97string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
 98OleDbConnection myConnection = new OleDbConnection(strCnn);
 99OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" + 
100"VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);
101
102System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter("@PersonName", System.Data.OleDb.OleDbType.VarChar,50); 
103paramPersonName.Value = fileTitle;
104command.Parameters.Add(paramPersonName);
105
106System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", System.Data.OleDb.OleDbType.VarChar,50);
107paramPersonEmail.Value = "mengxianhui@dotnet.aspx.cc";
108command.Parameters.Add(paramPersonEmail);
109
110System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", System.Data.OleDb.OleDbType.VarChar,50); 
111paramPersonSex.Value = "";
112command.Parameters.Add(paramPersonSex);
113
114System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", System.Data.OleDb.OleDbType.VarChar,50);
115paramPersonImageType.Value = fileType;
116command.Parameters.Add(paramPersonImageType);
117
118System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", System.Data.OleDb.OleDbType.Binary); 
119paramPersonImage.Value = fileData;
120command.Parameters.Add(paramPersonImage);
121
122//打开连接,执行查询
123myConnection.Open();
124command.ExecuteNonQuery();
125myConnection.Close();
126}
127}
128}
129
评论
  • Re:将文字内容和图片插入到Access数据库(转帖)  (2010/11/27 10:19:08) by midsummer 

    楼主可以尝试一下这个属性,Parameters.addwithvalue("@a",a);

标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.