Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用处理程序和SQL Server连接_C#_Asp.net_Sql Server 2008_Generic Handler - Fatal编程技术网

C# 通用处理程序和SQL Server连接

C# 通用处理程序和SQL Server连接,c#,asp.net,sql-server-2008,generic-handler,C#,Asp.net,Sql Server 2008,Generic Handler,我在泛型处理程序中有这段代码,用于连接到数据库并从表中检索图像 但是当用户登录时会选择数据库, 例如: my database is: SH001 user: username Password: password 登录时,我将数据库保存在会话[“db”]中 当用户进入产品页面并搜索产品时…它会列出产品及其图像 但由于我更改了以下连接: System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnec

我在泛型处理程序中有这段代码,用于连接到数据库并从表中检索图像

但是当用户登录时会选择数据库, 例如:

my database is: SH001

user: username

Password: password
登录时,我将数据库保存在
会话[“db”]

当用户进入产品页面并搜索产品时…它会列出产品及其图像

但由于我更改了以下连接:

System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=SH001;User ID=userid;Password=password");
致:

在产品页面中检索图像时,它开始给我一个错误

Object reference not set to an instance of an object.
这是通用处理程序中的代码:

public class image : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=" + HttpContext.Current.Session["db"] + ";User ID=userid;Password=pass");
        string image = context.Request.QueryString["ID"].ToString();
        SqlCommand com = new SqlCommand("Select Image from Products where ID = '" + image + "'", Con);
        Con.Open();

        SqlDataReader Reader = com.ExecuteReader();
        while (Reader.Read())
        {
            if (Reader.GetValue(0) != DBNull.Value)
            {
                context.Response.BinaryWrite((byte[])Reader["Image"]);
            }
        }
        Con.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

您需要做一些额外的工作才能从处理程序访问会话。请参阅此答案:可能重复的
public class image : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=" + HttpContext.Current.Session["db"] + ";User ID=userid;Password=pass");
        string image = context.Request.QueryString["ID"].ToString();
        SqlCommand com = new SqlCommand("Select Image from Products where ID = '" + image + "'", Con);
        Con.Open();

        SqlDataReader Reader = com.ExecuteReader();
        while (Reader.Read())
        {
            if (Reader.GetValue(0) != DBNull.Value)
            {
                context.Response.BinaryWrite((byte[])Reader["Image"]);
            }
        }
        Con.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}