A. sqlserver資料庫存儲的圖片格式(二進制數據)怎麼顯示到頁面
1.將圖片以二進制存入資料庫
//保存圖片到資料庫
protected void Button1_Click(object sender, EventArgs e)
{
//圖片路徑
string strPath = "~/photo/03.JPG";
string strPhotoPath = Server.MapPath(strPath);
//讀取圖片
FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
myComm.ExecuteNonQuery();
myConn.Close();
}
2.讀取二進制圖片在頁面顯示
//讀取圖片
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myConn.Open();
SqlDataReader dr = myComm.ExecuteReader();
while (dr.Read())
{
byte[] photo = (byte[])dr["personPhoto"];
this.Response.BinaryWrite(photo);
}
dr.Close();
myConn.Close();
或
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
this.Response.BinaryWrite(photo);
3.設置Image控制項顯示從資料庫中讀出的二進制圖片
---------------------------------------------
SqlConnection myConn = new SqlConnection("Data Source=192.168.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
//圖片路徑
string strPath = "~/photo/wangwu.JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
3.顯示圖片
this.Image1.ImageUrl = strPath;
4.GridView中ImageField以URL方式顯示圖片
--------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="圖片">
</asp:ImageField>
</Columns>
</asp:GridView>
5.GridView顯示讀出的二進制圖片
//樣板列
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="圖片">
</asp:ImageField>
<asp:TemplateField HeaderText="圖片">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
// System.ComponentModel.Container
string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");
Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");
if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))
{
//
byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");
//圖片路徑
string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
//顯示圖片
tmp_Image.ImageUrl = strPath;
}
}
B. 用VS2010+SqlServer2008做網站,圖片地址存在資料庫中,綁定ImageUrl,運行時圖片出不來,請大神幫幫忙!
不是e:\這樣的絕對路徑 而是相對路徑。
並且把圖片放到工程的目錄下面,
比如目錄images
那麼就寫成 images/圖片1.jpg
C. Access2010中如何運行SQL執行SQL語句
1、打開ACCESS資料庫,找到右邊的空數據,單擊彈出對話框,讓你選擇資料庫保存的路徑和名稱,大家根據自己的實際情況保存吧,我這里使用默認的。
D. 進入PLSQL的命令窗口,輸入SQL語句後,應該如何執行
1、首先在電腦中,找到並登錄PLSQL,如下圖所示。
E. 網頁製作SQL和圖片問題
你看下你數據存儲的圖片名字和網站下圖片文件夾里的圖片名字是否一致
F. 在運行SQL server2008時候出現下面圖片是怎麼回事
。。。。你把報警都抹掉了我們怎麼看。。。。
G. 請教利用SQL語句如何預覽圖片或者顯示圖片
直接用SQL語句是沒法顯示的﹐只能用其他的編程語言連接才能顯示
H. 在SQL Server2005中如何運行SQL語句
1、首先打開SQL Server 軟體,然後,單擊圖中的「資料庫」前面的「+」號,展開資料庫。
I. 首頁調用文章和圖片的sql語句寫法
select top 7 *
from zf11_News
order by ispic desc,
id desc
第一條是圖片,其餘六條當文本來使即可.
請參考http://www.it118.org/specials/c9fba99e-4401-49cf-8256-ac3c1a34c0d9/default.htm