sql server提供了一個特別的數據類型:image,它是一個包含binary數據的類型。下邊這個例子就向你展示了如何將文本或照片放入到數據庫中的辦法。在這篇文章中我們要看到如何在sql server中存儲和讀取圖片。
1、建立一個表:
在sql server中建立這樣結構的一個表:
列名 類型 目的
id integer 主鍵id
imgtitle varchar(50) 圖片的標題
imgtype varchar(50) 圖片類型. asp.net要以辨認的類型
imgdata image 用于存儲二進制數據
2、存儲圖片到sql server數據庫中
為了能存儲到表中,你首先要上傳它們到你的web 服務器上,你可以開發一個web form,它用來將客戶端中textbox web control中的圖片入到你的web服務器上來。將你的 enctype 屬性設置為:myltipart/formdata.
stream imgdatastream = file1.postedfile.inputstream;
int imgdatalen = file1.postedfile.contentlength;
string imgtype = file1.postedfile.contenttype;
string imgtitle = textbox1.text;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.read(imgdata,0,imgdatalen);
string connstr=((namevaluecollection)context.getconfig("appsettings"))["connstr"];
sqlconnection connection = new sqlconnection(connstr);
sqlcommand command = new sqlcommand
("insert into imagestore(imgtitle,imgtype,imgdata)
values ( @imgtitle, @imgtype,@imgdata )", connection );
sqlparameter paramtitle = new sqlparameter
("@imgtitle", sqldbtype.varchar,50 );
paramtitle.value = imgtitle;
command.parameters.add( paramtitle);
sqlparameter paramdata = new sqlparameter( "@imgdata", sqldbtype.image );
paramdata.value = imgdata;
command.parameters.add( paramdata );
sqlparameter paramtype = new sqlparameter( "@imgtype", sqldbtype.varchar,50 );
paramtype.value = imgtype;
command.parameters.add( paramtype );
connection.open();
int numrowsaffected = command.executenonquery();
connection.close();
3、從數據庫中恢復讀取
現在讓我們來從sql server中讀取我們放入的數據吧!我們將要輸出圖片到你的瀏覽器上,你也可以將它存放到你要的位置。
private void page_load(object sender, system.eventargs e)
{
string imgid =request.querystring["imgid"];
string connstr=((namevaluecollection)
context.getconfig("appsettings"))["connstr"];
string sql="select imgdata, imgtype from imagestore where id = " + imgid;
sqlconnection connection = new sqlconnection(connstr);
sqlcommand command = new sqlcommand(sql, connection);
connection.open();
sqldatareader dr = command.executereader();
if(dr.read())
{
response.contenttype = dr["imgtype"].tostring();
response.binarywrite( (byte[]) dr["imgdata"] );
}
connection.close();
}
要注意的是response.binarywrite 而不是response.write.
下面給大家一個用于c# winform的存入、讀取程序。其中不同請大家自己比較!(為了方便起見,我將數據庫字段簡化為二個:imgtitle和imgdata。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.io;
using system.data.sqlclient;
namespace windowsapplication21
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.button button1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
private string connectionstring = "integrated security=sspi;initial catalog=;data source=localhost;";
private sqlconnection conn = null;
private sqlcommand cmd = null;
private system.windows.forms.button button2;
private system.windows.forms.picturebox pic1;
private system.windows.forms.openfiledialog openfiledialog1;
private string sql = null;
private system.windows.forms.label label2;
private string nowid=null;
public form1()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
conn = new sqlconnection(connectionstring);
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if (conn.state == connectionstate.open)
conn.close();
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
新聞熱點
疑難解答
圖片精選