在 C# 中从数据库检索图像到数据网格

发布于 2025-01-06 06:23:56 字数 2236 浏览 3 评论 0原文

我将图像以二进制格式保存到数据库中。当我尝试检索它时出现错误。该错误显示“在应用程序配置中找不到连接名称“ConnectionString”或连接字符串为空。”这是我的代码,我做错了什么?

//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 =@ID";
    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>

I saved the image into the database in binary format. I get an error when i am trying to retrieve it. The error says "The connection name 'ConnectionString' was not found in the applications configuration or the connection string is empty." This is my code, what am i doing wrong??

//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 =@ID";
    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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2025-01-13 06:23:56

“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;

"Data Source=ACER-PC\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" is your Connection String.

Check your web.config and see if you have something like this:

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

After that, change your line to

con.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
太阳公公是暖光 2025-01-13 06:23:56

错误:

con.ConnectionString = ConfigurationManager.ConnectionStrings["数据源=ACER-
PC\SQLEXPRESS;初始目录=imageDemo;集成
Security=True"
].ConnectionString;

应该是..

con.ConnectionString = "Data Source=ACER-PC\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True";

或者

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"/>

Wrong:

con.ConnectionString = ConfigurationManager.ConnectionStrings["Data Source=ACER-
PC\SQLEXPRESS;Initial Catalog=imageDemo;Integrated
Security=True"
].ConnectionString;

It should be..

con.ConnectionString = "Data Source=ACER-PC\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True";

Or

con.ConnectionString = ConfigurationManager.ConnectionStrings["YourConnection"].ConnectionString;

So, you need to put following in your app.config or web.config

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文