我以二進制格式將圖像保存到數據庫中。當我嘗試檢索它時,出現錯誤。錯誤顯示「連接名稱'ConnectionString'在應用程序配置中找不到,或連接字符串爲空。」這是我的代碼,我做錯了什麼?從c#中的數據庫檢索圖像到數據網格#
//Code for generic handler
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Data Source=ACER-
PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated
Security=True"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ImageName,Image from image" + " where ID [email protected]";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Image"]);
dReader.Close();
con.Close();
}
//Code for datagrid
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [ImageName], [Image]
FROM [image]"></asp:SqlDataSource>
現在,它的工作原理如下。謝謝 – 2012-02-17 06:41:44
@Sindu_請將此標記標記爲[答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):)。 – 2012-02-17 16:48:12