【发布时间】:2011-05-16 21:41:25
【问题描述】:
这是对我之前问过的question 的回应,因为我不能在 8 小时之前在那里发布,所以我创建了一个新问题,解决我面临的问题,
代码如下:
if (context.Request.QueryString["id"] != null)
{
int id = int.Parse(context.Request.QueryString["id"].ToString());
string connectionString = "Data Source=tarun-PC\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";
string commandString = "Select fsImage from WHATSNSTORE_FEATURED_STORES where imageId=" + id;
SqlConnection oConnection = new SqlConnection(connectionString);
SqlCommand oCommand = new SqlCommand(commandString, oConnection);
if (oConnection.State != ConnectionState.Open)
{
oConnection.Open();
}
SqlDataReader myDataReader = oCommand.ExecuteReader();
//myDataReader.Read();
try
{
if (myDataReader.Read())
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte[])myDataReader["fsImage"]);
}
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
我的桌子
create table WHATSNSTORE_FEATURED_STORES
(
imageId int primary key identity,
fsImage image
)
现在唯一的问题是,在调试时它跳过了if(myDataReader.Read()) 部分,这表明没有数据存在!
我该如何解决这个问题?
【问题讨论】:
-
您有错误返回吗?在 context.Response.Write(ex.Message); 你有什么吗?在那条线上放一个断点,然后检查你的应用程序是否在那里...
-
如果没有数据,说明你需要获取数据。
-
@Marco 没有 if(),当我运行代码时,我得到的错误是“无效的读取尝试,当没有数据存在时”.. 再次,我运行了查询在 SSMS 中,我得到了想要的结果!
标签: c# asp.net sqldatareader