我在通用处理程序中有这个代码来连接到数据库并从表中检索图像,
但是当用户登录时会选择数据库,例如:
my database is: SH001
user: username
Password: password
Session["db"]
我在登录时保存数据库
当用户进入产品页面并搜索产品时......它会列出带有图片的产品......
但因为我改变了连接:
System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=SH001;User ID=userid;Password=password");
至:
System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=" + HttpContext.Current.Session["db"] + ";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;
}
}
}