介紹
有時候我們需要保存一些binary data進數據庫
建表
為了試驗這個例子你需要一個含有數據的table(你可以在現在的庫中創建它
Column Name
Datatype
Purpose
ID
Integer
identity column Primary key
IMGTITLE
Varchar(
Stores some user friendly title to identity the image
IMGTYPE
Varchar(
Stores image content type
IMGDATA
Image
Stores actual image or binary data
保存images進SQL Server數據庫
為了保存圖片到table你首先得從客戶端上傳它們到你的web服務器
Stream imgdatastream = File
int imgdatalen = File
string imgtype = File
string imgtitle = TextBox
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream
string connstr=
((NameValueCollection)Context
(
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand
(
VALUES ( @imgtitle
SqlParameter paramTitle = new SqlParameter
(
paramTitle
command
SqlParameter paramData = new SqlParameter
(
paramData
command
SqlParameter paramType = new SqlParameter
(
paramType
command
connection
int numRowsAffected = command
connection
從數據庫中輸出圖片
現在讓我們從數據庫中取出我們剛剛保存的圖片
private void Page_Load(object sender
{
string imgid =Request
string connstr=((NameValueCollection)
Context
string sql=
+ imgid;
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql
connection
SqlDataReader dr = command
if(dr
{
Response
Response
}
connection
}
在上面的代碼中我們使用了一個已經打開的數據庫
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22068.html