2012-02-17 443 views
0

我以二進制格式將圖像保存到數據庫中。當我嘗試檢索它時,出現錯誤。錯誤顯示「連接名稱'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> 

回答

3

「Data Source = ACER-PC \ SQLEXPRESS; Initial Catalog = imageDemo; Integrated Security = True」是您的連接字符串。

檢查你的web.config,看看你是否有這樣的事情:

<connectionStrings> 
    <add name="myConnection" connectionString="Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

之後,您的生產線改爲

con.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString; 
+0

現在,它的工作原理如下。謝謝 – 2012-02-17 06:41:44

+0

@Sindu_請將此標記標記爲[答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):)。 – 2012-02-17 16:48:12

2

錯誤:

con.ConnectionString = ConfigurationManager中.ConnectionStrings [「Data Source = ACER-
PC \ SQLEXPRESS; Initial Catalog = imageDemo; I ntegrated 安全= True「
] .ConnectionString;

應該..

con.ConnectionString = 「數據源= ACER-PC \ SQLEXPRESS;初始目錄= imageDemo;集成安全性=真」;

或者

con.ConnectionString = ConfigurationManager.ConnectionStrings [ 「YourConnection」]的ConnectionString。

所以,你需要把你的app.config或web.config中

<connectionStrings> 
<add name="YourConnection" connectionString="Data Source=ACER-PC\\SQLEXPRESS;Initial  Catalog=imageDemo;Integrated Security=True" providerName="System.Data.SqlClient"/>