NO IMAGE

Image2Access.aspx

<%@ Page language=”c#” Debug=”true” Codebehind=”Image2Access.aspx.cs”
AutoEventWireup=”false” Inherits=”eMeng.Exam.Image2Access” %>
<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >
<html>
<head>
<title>上傳檔案到 Access 資料庫</title>
<meta name=”GENERATOR” content=”Microsoft Visual Studio 7.0″ />
<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 ms_positioning=”GridLayout”>
<form id=”DataGridShowImage” method=”post” runat=”server” enctype=”multipart/form-data”>
<h3 align=”center”>上傳檔案到 Access 資料庫</h3>
<asp:DataGrid ID=”DG_Persons” AutoGenerateColumns=”False” Width=”99%” HeaderStyle-BackColor=”#ff0000″
HeaderStyle-Font-Bold=”True” HeaderStyle-ForeColor=”#ffffff” ItemStyle-BackColor=”Beige” BorderColor=”#000000″
Runat=”server” HeaderStyle-HorizontalAlign=”Center”>
<columns>
<asp:TemplateColumn headertext=”姓名”>
<itemtemplate>
<asp:Label Runat=”server” Text='<%# DataBinder.Eval(Container.DataItem, “PersonName”) %>’ ID=”Label1″/>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext=”電子郵件”>
<itemtemplate>
<asp:Label Runat=”server” Text='<%# DataBinder.Eval(Container.DataItem, “PersonEmail”) %>’ ID=”Label2″/>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext=”性別”>
<itemtemplate>
<asp:Label Runat=”server” Text='<%# DataBinder.Eval(Container.DataItem, “PersonSex”) %>’ ID=”Label3″/>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext=”照片”>
<itemtemplate>
<asp:Image Runat=server ID=”Image1″
ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, “PersonID”)) %>’ />
</itemtemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
<strong>檔名字:</strong><input id=”MyFileName” type=”text” runat=”server” NAME=”MyFileName” />
<p>
<strong>檔案:</strong><input id=”MyFile” type=”file” runat=”server” NAME=”MyFile” />
<br />
<br />
<input type=”submit” value=”開始上傳” runat=”server” ID=”Submit1″ NAME=”Submit1″ />
</p>
</form>
</body>
</html>

Image2Access.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace eMeng.Exam
{
/// <summary>
/// Image2Access 的摘要說明。
/// </summary>
public class Image2Access : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected System.Web.UI.WebControls.DataGrid DG_Persons;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者程式碼以初始化頁面
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);
myCommand.CommandType = CommandType.Text;
try
{
myConnection.Open();
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DG_Persons.DataBind();
}
catch(OleDbException SQLexc)
{
Response.Write(“提取資料時出現錯誤:” SQLexc.ToString());
}
}
protected string FormatURL(object strArgument)
{
return “ReadImage.aspx?id=” strArgument.ToString();
}
#region Web 窗體設計器生成的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該呼叫是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支援所需的方法 – 不要使用程式碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Submit1.ServerClick = new System.EventHandler(this.Submit1_ServerClick);
this.Load = new System.EventHandler(this.Page_Load);
}
#endregion
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
//得到提交的檔案
Stream fileDataStream = MyFile.PostedFile.InputStream;
//得到檔案大小
int fileLength = MyFile.PostedFile.ContentLength;
//建立陣列
byte[] fileData = new byte[fileLength];
//把檔案流填充到陣列
fileDataStream.Read(fileData,0,fileLength);
//得到檔名字
string fileTitle = MyFileName.Value;
//得到檔案型別
string fileType = MyFile.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);
System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter(“@PersonName”, System.Data.OleDb.OleDbType.VarChar,50);
paramPersonName.Value = fileTitle;
command.Parameters.Add(paramPersonName);
System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter(“@PersonEmail”, System.Data.OleDb.OleDbType.VarChar,50);
paramPersonEmail.Value = “mengxianhui@dotnet.aspx.cc”;
command.Parameters.Add(paramPersonEmail);
System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter(“@paramPersonSex”, System.Data.OleDb.OleDbType.VarChar,50);
paramPersonSex.Value = “男”;
command.Parameters.Add(paramPersonSex);
System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter(“@PersonImageType”, System.Data.OleDb.OleDbType.VarChar,50);
paramPersonImageType.Value = fileType;
command.Parameters.Add(paramPersonImageType);
System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter(“@PersonImage”, System.Data.OleDb.OleDbType.Binary);
paramPersonImage.Value = fileData;
command.Parameters.Add(paramPersonImage);
//開啟連線,執行查詢
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();
}
}
}