在 C# 中从数据库检索图像到数据网格
我将图像以二进制格式保存到数据库中。当我尝试检索它时出现错误。该错误显示“在应用程序配置中找不到连接名称“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“Data Source=ACER-PC\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True”是您的连接字符串。
检查你的 web.config 并看看是否有类似这样的内容:
之后,将你的行更改为
"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:
After that, change your line to
错误:
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 中
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