Ⅰ 怎麼在sql中保存圖片
/*--bcp 實現二進制文件的導入導出
支持image,text,ntext欄位的導入/導出
image適合於二進制文件,包括:Word文檔,Excel文檔,圖片,音樂等
text,ntext適合於文本數據文件
注意:導入時,將覆蓋滿足條件的所有行
導出時,將把所有滿足條件的行導出到指定文件中
此存儲過程僅用bcp實現
鄒建 2003.08-----------------*/
/*--調用示例
--數據導出
exec p_binaryIO 'zj','','','acc_演示數據..tb','img','c:\zj1.dat'
--數據導入
exec p_binaryIO 'zj','','','acc_演示數據..tb','img','c:\zj1.dat','',0
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_binaryIO]') and OBJECTPROPERTY(id, N'IsProcere') = 1)
drop procere [dbo].[p_binaryIO]
GO
Create proc p_binaryIO
@servename varchar (30),--伺服器名稱
@username varchar (30), --用戶名
@password varchar (30),--密碼
@tbname varchar (500), --資料庫..表名
@fdname varchar (30), --欄位名
@fname varchar (1000),--目錄+文件名,處理過程中要使用/覆蓋:@filename+_temp
@tj varchar (1000)='', --處理條件.對於數據導入,如果條件中包含@fdname,請指定表名前綴
@isout bit=1--1導出((默認),0導入
AS
declare @fname_in varchar(1000)--bcp處理應答文件名
,@fsize varchar(20)--要處理的文件的大小
,@m_tbname varchar(50)--臨時表名
,@sql varchar(8000)
--則取得導入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可選名 varchar(20),大小 int
,創建日期 varchar(10),創建時間 varchar(20)
,上次寫操作日期 varchar(10),上次寫操作時間 varchar(20)
,上次訪問日期 varchar(10),上次訪問時間 varchar(20),特性 int)
insert into #tb
exec master..xp_getfiledetails @fname
select @fsize=大小 from #tb
drop table #tb
if @fsize is null
begin
print '文件未找到'
return
end
end
--生成數據處理應答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '+@m_tbname+' from(
select null as 類型
union all select 0 as 前綴
union all select '+@fsize+' as 長度
union all select null as 結束
union all select null as 格式
) a'
exec(@sql)
select @fname_in=@fname+'_temp'
,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--刪除臨時表
set @sql='drop table '+@m_tbname
exec(@sql)
if @isout=1
begin
set @sql='bcp "select top 1 '+@fdname+' from '
+@tbname+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
+'" queryout "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--為數據導入准備臨時表
set @sql='select top 0 '+@fdname+' into '
+@m_tbname+' from ' +@tbname
exec(@sql)
--將數據導入到臨時表
set @sql='bcp "'+@m_tbname+'" in "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
--將數據導入到正式表中
set @sql='update '+@tbname
+' set '+@fdname+'=b.'+@fdname
+' from '+@tbname+' a,'
+@m_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
exec(@sql)
--刪除數據處理臨時表
set @sql='drop table '+@m_tbname
end
--刪除數據處理應答文件
set @sql='del '+@fname_in
exec master..xp_cmdshell @sql
go
Ⅱ 怎樣把圖片存入SQL資料庫表中
一、先來熟悉一下將要使用的對象方法:
用來獲取上一個頁面傳 遞過來的數據一般是使用Request對象。同樣的,我們也可以使用Request對象 來獲取上傳上來的文件數據,使用的方法是Request.BinaryRead()。
要從資料庫中讀出來圖片的數據顯示到網頁上面要用到的方法是:Request.BinaryWrite()。
二、在得到了圖片的數據,要保存到資料庫中的時候,不可以直接使用Insert語句對資料庫進行操作,而是要使用ADO的AppendChunk方法。
同樣的,讀出資料庫中的圖片數據,要使用GetChunk方法。
各個方法的具體語法如下:
*Request.BinaryRead語法:
variant=Request.BinaryRead(count)
參數
variant
返回值保存著從客戶端讀取到數據。
count
指明要從客戶端讀取的數據量大小,這個值小於或者等於使用方法
Request.TotalBytes得到的數據量。
*Request.BinaryWrite語法:
Request.BinaryWritedata
參數
data
要寫入到客戶端瀏覽器中的數據包。
*Request.TotalBytes語法:
variant=Request.TotalBytes
參數
variant
返回從客戶端讀取到數據量的位元組數。
*AppendChunk語法
將數據追加到大型文本、二進制數據Field或Parameter對象。
object.AppendChunkData
參數
objectField或Parameter對象
Data變體型,包含追加到對象中的數據。
說明
使用Field或Parameter對象的AppendChunk方法可將長二進制或字元數
據填寫到對象中。在系統內存有限的情況下,可以使用AppendChunk方法對長
整型值進行部分而非全部的操作。
*GetChunk語法
返回大型文本或二進制數據Field對象的全部或部分內容。
variable=field.GetChunk(Size)
返回值
返回變體型。
參數
Size長整型表達式,等於所要檢索的位元組或字元數。
說明
使用Field對象的GetChunk方法檢索其部分或全部長二進制或字元數據。
在系統內存有限的情況下,可使用GetChunk方法處理部分而非全部的長整型
值。
GetChunk調用返回的數據將賦給「變數」。如果Size大於剩餘的數據,則
GetChunk僅返回剩餘的數據而無需用空白填充「變數」。如果欄位為空,則
GetChunk方法返回Null。
每個後續的GetChunk調用將檢索從前一次GetChunk調用停止處開始的數
據。但是,如果從一個欄位檢索數據然後在當前記錄中設置或讀取另一個欄位
的值,ADO將認為已從第一個欄位中檢索出數據。如果在第一個欄位上再次調
用GetChunk方法,ADO將把調用解釋為新的GetChunk操作並從記錄的起始
處開始讀取。如果其他Recordset對象不是首個Recordset對象的副本,則
訪問其中的欄位不會破壞GetChunk操作。
如果Field對象的Attributes屬性中的adFldLong位設置為True,則可
以對該欄位使用GetChunk方法。
如果在Field對象上使用Getchunk方法時沒有當前記錄,將產生錯誤3021
(無當前記錄)。
三、設計資料庫,作為測試的資料庫結構如下(Access97):
欄位名稱類型描述
id自動編號主鍵值
imgOLE對象用來保存圖片數據
對於在MSSQLServer7中,對應的結構如下:
欄位名稱類型描述
idint(Identity)主鍵值
imgimage用來保存圖片數據
四、正式編寫純ASP代碼上傳部分了,首先,有一個提供給用戶的上傳界面,可以讓用戶選擇要上傳的圖片。代碼如下(upload.htm):
<html>
<body>
<center>
<formname="mainForm"enctype="multipart/form-data"
action="process.asp"method=post>
<inputtype=filename=mefile><br>
<inputtype=submitname=okvalue="OK">
</form>
</center>
</body>
</html>
注意代碼中黑色斜體的部分,一定要在Form中有這個屬性,否則,將無
法得到上傳上來的數據。
五、接下來,要在process.asp中對從瀏覽器中獲取的數據進行必要的處理,因為在process.asp中獲取到的數據不僅僅包含了想要的上傳上來的圖片的數據,也包含了其他的無用的信息,需要剔除冗餘數據,並將處理過的圖片數據保存到資料庫中,這里以Access97為例。具體代碼如下(process.asp):
<%
response.buffer=true
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13)&chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf&bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&
server.MapPath("images.mdb")&";uid=;PWD=;"
connGraph.Opensetrec=server.createobject("ADODB.recordset")
rec.Open"SELECT*FROM[images]whereidisnull",connGraph,1,3
rec.addnew
rec("img").appendchunkmydata
rec.update
rec.close
setrec=nothing
setconnGraph=nothing
%>
六、這樣就把上傳來的圖片保存到了名為images.mdb的資料庫中了,剩下的工作就是要將資料庫中的圖片數據顯示到網頁上面了。
一般在HTML中,顯示圖片都是使用<IMG>標簽,也就是<IMGSRC="圖片路徑">,但是圖片是保存到了資料庫中,「圖片路徑」是什麼呢?呵呵,其實這個SRC屬性除了指定路徑外,也可以這樣使用哦:
<IMGSRC="showimg.asp?id=xxx">
所以,要做的就是在showimg.asp中從資料庫中讀出來符合條件的數據,並返回到SRC屬性中就可以了,具體代碼如下(showimg.asp):
<%
setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&
server.MapPath("images.mdb")&";uid=;PWD=;"
connGraph.Open
setrec=server.createobject("ADODB.recordset")
strsql="selectimgfromimageswhereid="&trim(request("id"))
rec.openstrsql,connGraph,1,1
Response.ContentType="image/*"
Response.BinaryWriterec("img").getChunk(7500000)
rec.close
setrec=nothing
setconnGraph=nothing
%>
七、注意在輸出到瀏覽器之前一定要指Response.ContentType="image/*",以便正常顯示圖片。最後要注意的地方是,我的process.asp中作的處理沒有考慮到第一頁(upload.htm)中還有其他數據,比如<INPUTtype=tesxtname=userid>等等,如果有這些項目,process.asp就要注意處理掉不必要的數據。
Ⅲ sql2008中怎樣存放圖片
二種方式來存放圖片:
先建立數據表imagetable(表所在資料庫為tempdb)
create table imagetable
(
imagefile nvarchar(200),
imagedata varbinary(max)
)
在程序中讀取位元組流後,再插入到資料庫
讀取文件二進制數據格式內容
byte[] imagebytes =null;
stringfullpath = @"E:iWorkspaceImages est.gif";
FileStreamfs =newFileStream(fullpath,FileMode.Open,FileAccess.Read);
BinaryReaderbr =newBinaryReader(fs);
imagebytes = br.ReadBytes(fs.Length);
插入到數據表:
cmd.CommandText =@" insert into imagetable values (@imagefile, @imagedata)";
cmd.Parameters.AddWithValue("@imagefile",fullpath);
cmd.Parameters.AddWithValue("@imagedata",imagebytes);
cmd.ExecuteNonQuery();
2. 如果文件本身和SQL在同一台機器上,則可使用OPENROWSET來讀取文件
INSERT INTO imagetable(imagefile,imagedata)
SELECT 'filepath',* FROM
OPENROWSET(BULK N'E:iWorkspaceImages est.gif', SINGLE_BLOB) AS Photo
註:獲取BLOB欄位後必須用欄位別名命名,如 As Photo,否則會有以下錯誤:必須在 FROM 子句中為大容量行集指定相關名稱。
Ⅳ 怎麼在sql資料庫中存放圖片
VB在SQL Server 2000中存儲圖片,其實不是特別困難的。
1、數據表必須有數據類型是Image類型的欄位,這個欄位是可以存儲圖形的二進制數據的,存儲量可達2G位元組。
2、可想,存儲圖形二進制數據,必須就原來的圖形轉換為二進制數據,這是存儲圖形數據的關鍵。
3、存儲的二進制圖形,如果要讀取,必須就二進制數據轉換為圖形數據。
上面是VB在SQL Server 2000中存儲圖片的必須要求。但是也可以在數據表存儲圖形的路徑,這樣比較簡單,但是不安全,我們就以存儲二進制數據討論吧。
一、存儲圖形的關鍵語句:
Dim mst As New ADODB.Stream 'Stream 對象是進行二進制數據操作對象
mst.Type = abTypeBinary
mst.Open
If 圖片的路徑和文件名變數(需要用其他方法獲得)> "" Then
mst.LoadFromFile 圖片的路徑和文件名變數
End If
rs("存儲二進制數據的欄位") = mst.Read
二、讀取存儲的二進制數據的關鍵語句:
Dim mst As New ADODB.Stream
mst.Type = abTypeBinary
mst.Open
mst.Write rs("存儲二進制數據的欄位")
mst.SaveToFile App.Path & "/" & list1.Text,abSaveCreateOverWrite
Picture1.Picture = LoadPicture(App.Path & "/" & List1.Text)
mst.Close '別忘了關閉對象!
Ⅳ SQL資料庫中如何存儲圖片和讀取
要看你用哪種語言,但是流程都差不多。都是把圖片變成留,然後變成位元組數組,然後再保存到資料庫裡面去。存地址確實是個辦法,但是卻不實用。
Ⅵ sql 可以存圖片
可以的,但是最好將圖片放到單獨的文件裡面,將圖片路徑保留在資料庫中
Ⅶ 如何在sql server中存儲圖片
1、首先可以存儲圖片鏈接,設置圖片鏈接欄位,如下圖所示。
Ⅷ SQL中,存儲圖片用哪種類型
上傳圖片一般不存放到資料庫中,非要存儲可選擇BOLB之類的類型