當前位置:首頁 » 操作系統 » 資料庫怎麼讀取圖片

資料庫怎麼讀取圖片

發布時間: 2022-04-17 17:06:59

資料庫以img存儲,如何讀取圖片

直接使用企業管理器好像沒有辦法操作吧,通過軟體或自己做個小軟體讀取。

#region //讀取資料庫中圖片到內存.並顯示
public void LoadToMemoryAndDisable(string serverAdress, string database)
{
//讀取資料庫中圖片到內存.並顯示
sqlConnection conn = new SqlConnection("server=" + serverAdress + ";integrated security = sspi;database = " + database);
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在內存中操作圖片數據
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
#endregion

⑵ 怎樣讀取資料庫中存儲的二進制圖片文件

下面我們將示例一個圖片文件讀取存儲至資料庫並從資料庫中讀取圖片信息並顯示的案例:
1、首先讀取硬碟上的某一具體圖片文件,讀取模式設置為readBinary方式:
<cffile
action
=
"readBinary"
file
=
"temp
directory
here#file.serverFile#"
variable
=
"test">
2、將讀取出來的二進制內容存儲至資料庫中(註:資料庫欄位需設置成能存儲圖片類型的欄位,如blob類型):
<cfquery
datasource
=
"datasource">
insert
into
imageTest
values
(<cfqueryparam
cfsqltype="cf_sql_blob"
value="#test#">)
</cfquery>
通過1、2兩個步驟,我們輕松實現了讀取圖片文件並存儲至資料庫的操作過程。
3、從資料庫中讀取圖片信息,該文件可命名為dispImage.cfm:
<!---
在此需特別注意enablecfoutputonly的壓縮空白功能,如果不對該頁面進行空白壓縮,很可能會造成圖片無法顯示的問題
--->
<cfprocessingdirective
suppressWhiteSpace="yes">
<cfsetting
enablecfoutputonly="yes">
<!---
讀取相應的圖片信息
--->
<cfquery
datasource
=
"datasource">
select
image
from
imageTest
where
variable
here#
</cfquery>
<!---
設置瀏覽器輸出的格式,我們將它設置為圖片的JPG類型,用戶可根據實際情況改動類型設置
--->
<cfcontent
type="image/jpg">
<!---
輸出圖片
--->
<cfoutput>#toString(imageTest.image)#</cfoutput>
</cfprocessingdirective>
<cfabort>
4、顯示圖片內容,調用dispImage.cfm頁面:
<img
src
=
"dispImage.cfm?id=your
variable
here">
通過3、4兩個步驟,我們也很容易的就完成了從資料庫中讀取圖片信息並在頁面顯示的功能。
總結:實際上,除了圖片文件可以如此處理,其它的文件也能通過類似方式進行處理,可將任意文件類型存儲至資料庫,只是文件大小的原因以及資料庫存儲讀取速度性能限制,我們基本上還是不建議將文件存儲至資料庫,畢竟硬碟讀取要快得多。

⑶ 怎樣從SQL資料庫中讀取圖片,將他存為文件

Public Function ReadFromDb(ByRef conn As ADODB.Connection, ByVal strTbl As String, ByVal strFl As String, ByVal strCondi As String) As Boolean
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "select top 1 * from " & strTbl & " where " & strCondi, conn, 3, 2
If rs.EOF Or IsNull(rs(strFl)) Then
ReadFromDb = False
Exit Function
End If

Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields(strFl).Value
mstream.SaveToFile "c:\carrot.gif", adSaveCreateOverWrite
rs.Close
ReadFromDb = True
End Function

⑷ 如何才能往資料庫里讀取圖片數據或者從資料庫里讀圖片能告訴我具體步驟嗎謝謝

具體步驟:
1.連接資料庫
2.查詢資料庫
3.調用資料庫中的圖片(有些是按照地址保存,有的是按照二進制保存)
在調用的地方用<img src="<%=rs("存放圖片的欄位")%>">
這樣就可以了

⑸ 從資料庫中讀取照片

SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader read = command.ExecuteReader();
byte[] bytes = (byte[])read["userportrait"];
MemoryStream stream = new MemoryStream(bytes);
BinaryFormatter translate = new BinaryFormatter();
picHost.Image = (Image)translate.Deserialize(stream);
直接復制的沒具體修改 這個是讀取的

php中如何從資料庫中讀取圖片

<?php

//將圖片存進資料庫再讀出,注意存儲圖片的欄位類型必須為blob
$user=』root』;
$password=』root』;
$db=』test』;
$connect=mysql_connect(『localhost』,$user,$password);
mysql_set_charset(『utf8′,$connect);
mysql_select_db($db);

$photo = 「0x」.bin2hex(file_get_contents(「./test.jpg」));
$sql=」INSERT INTO `test`.`test` (`photo`) VALUES ($photo);」;//$photo不需要用引號,切記
mysql_query($sql);

//$result=mysql_query(「SELECT *
//FROM `test`
//LIMIT 0 , 30〃);
//$img=mysql_fetch_array($result);
//echo $img['photo'];
?>

⑺ 圖片怎麼從資料庫里讀取出來

推薦你先看這篇文章:
使用asp.net將圖片上傳並存入SqlServer中,然後從SqlServer中讀取並顯示出來
一,上傳並存入SqlServer
資料庫結構
create table test
{
id identity(1,1),
FImage image
}
相關的存儲過程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO

在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上傳"></asp:Button>

然後在後置代碼文件UpPhoto.aspx.cs添加btnAdd按鈕的單擊事件處理代碼:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//獲得圖象並把圖象轉換為byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);

//連接資料庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcere;

cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;

//如果你希望不使用存儲過程來添加圖片把上面四句代碼改為:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

二,從SqlServer中讀取並顯示出來
在需要顯示圖片的地方添加如下代碼:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>

ShowPhoto.aspx主體代碼:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select * from test where id=2";//這里假設獲取id為2的圖片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}

⑻ mysql資料庫讀取圖片

讀出二進制,轉化成流,然後write到頁面上 byte[] blob = userinfo.getPhoto(); if(blob!=null){ response.reset(); response.setContentType("image/"+dat);

⑼ 如何才能往資料庫里讀取圖片數據或者從資料庫里讀圖片

王大偉,這題我不要了,你隨意認證,任意拒絕,我已舉報。

熱點內容
倒三角java編譯 發布:2024-09-30 05:05:39 瀏覽:17
電腦文件夾怎麼發到qq 發布:2024-09-30 04:37:48 瀏覽:651
蘋果手機網路緩存在哪裡清除安卓 發布:2024-09-30 04:20:52 瀏覽:408
spl資料庫 發布:2024-09-30 04:15:09 瀏覽:90
車輛解壓需要什麼材料 發布:2024-09-30 04:02:15 瀏覽:573
重差演算法 發布:2024-09-30 04:00:48 瀏覽:105
怎麼設置華為5700交換機配置 發布:2024-09-30 03:47:26 瀏覽:613
大數據存儲問題 發布:2024-09-30 03:42:25 瀏覽:262
舊電腦如何做游戲伺服器 發布:2024-09-30 03:42:25 瀏覽:374
windows伺服器和linux伺服器 發布:2024-09-30 03:33:14 瀏覽:401