此問題經常被人問
Access數據庫代碼
<%@ Page Language=
<%@ Import Namespace=
<!DOCTYPE html PUBLIC
<script runat=
protected void Button
{
System
if (fileDataStream
{
Msg
return;
}
//得到文件大小
int fileLength = FileUpload
//創建數組
byte[] fileData = new byte[fileLength];
//把文件流填充到數組
fileDataStream
//得到文件類型
string fileType = FileUpload
//構建數據庫連接
string strCnn =
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand(
command
command
command
command
command
//打開連接
myConnection
command
myConnection
Response
}
protected void Page_Load( object sender
{
if (!Page
{
BindGrid();
}
}
private void BindGrid( )
{
string strCnn =
+ Server
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand myCommand = new OleDbCommand(
try
{
myConnection
GridView
GridView
}
catch (OleDbException SQLexc)
{
Response
}
}
protected string FormatURL( object strArgument )
{
return
}
</script>
<html xmlns=
<head runat=
<title>上傳文件到數據庫</title>
</head>
<body>
<form id=
<asp:GridView ID=
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Eval(
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%#Eval(
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%#Eval(
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img src=
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
姓名
<br />
照片
<asp:Button ID=
<p>
<asp:Label ID=
</form>
</body>
</html>
SQL Server數據庫代碼
<%@ Page Language=
<%@ Import Namespace=
<!DOCTYPE html PUBLIC
<script runat=
string strCnn =
protected void Button
{
System
if (fileDataStream
{
Msg
return;
}
//得到文件大小
int fileLength = FileUpload
//創建數組
byte[] fileData = new byte[fileLength];
//把文件流填充到數組
fileDataStream
//得到文件類型
string fileType = FileUpload
//構建數據庫連接
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand(
command
command
command
//打開連接
myConnection
command
myConnection
Response
}
protected void Page_Load( object sender
{
if (!Page
{
BindGrid();
}
}
private void BindGrid( )
{
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand myCommand = new SqlCommand(
try
{
myConnection
GridView
GridView
}
catch (Exception SQLexc)
{
Response
}
}
protected string FormatURL( object strArgument )
{
return
}
</script>
<html xmlns=
<head id=
<title>上傳文件到數據庫</title>
</head>
<body>
<form id=
<asp:GridView ID=
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Eval(
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img src=
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
姓名
<br />
照片
<asp:Button ID=
<p>
<asp:Label ID=
</form>
</body>
</html>
顯示圖片
<%@ Page Language=
<%@ Import Namespace=
<%@ Import Namespace=
<script runat=
protected void Page_Load( object sender
{
////構建數據庫連接
//ACCESS數據庫使用本注釋部分
//string strCnn =
//OleDbConnection myConnection = new OleDbConnection(strCnn);
//OleDbCommand command = new OleDbCommand(
//myConnection
//OleDbDataReader dr = command
//if (dr
//{
// Response
// Response
// Response
//}
//dr
//myConnection
//構建數據庫連接
string strCnn =
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand(
myConnection
SqlDataReader dr = command
if (dr
{
Response
Response
Response
}
dr
myConnection
}
</script>
創建SQL數據表語句
CREATE TABLE [UserPhoto] (
[id] [int] IDENTITY (
[UserName] [nvarchar] (
[ContentType] [varchar] (
[Photo] [image] NOT NULL
CONSTRAINT [PK_UserPhoto] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
From:http://tw.wingwit.com/Article/program/net/201311/13767.html