protected void Page_Load(object sender,EventArgs e)
{ ///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM Pictures WHERE PictureID='1'";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{ ///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{ ///抛出异常
throw new Exception(ex.Message,ex);
}
///定义保存数据的byte数组
byte[] Data = null;
while(dr.Read())
{ ///读取数据
Data = (byte[])dr["Data"];
Response.ContentType = dr["Type"].ToString();
}
dr.Close();
//显示图片数据
this.EnableViewState = false;
///输出文件头
Response.AppendHeader("Content-Length",Data.Length.ToString());
///输出文件的数据
Response.BinaryWrite(Data);
///结束输出
Response.End();
}
//////////////////Data = (byte[])dr["Data"];
Response.ContentType = dr["Type"].ToString();
这两行有错误,提示超出确保列表中的最大索引小于列表的大小,确保数据列名正确,确保索引不是负数,请高人指点下上面的代码到底错在哪里?
{ ///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM Pictures WHERE PictureID='1'";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{ ///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{ ///抛出异常
throw new Exception(ex.Message,ex);
}
///定义保存数据的byte数组
byte[] Data = null;
while(dr.Read())
{ ///读取数据
Data = (byte[])dr["Data"];
Response.ContentType = dr["Type"].ToString();
}
dr.Close();
//显示图片数据
this.EnableViewState = false;
///输出文件头
Response.AppendHeader("Content-Length",Data.Length.ToString());
///输出文件的数据
Response.BinaryWrite(Data);
///结束输出
Response.End();
}
//////////////////Data = (byte[])dr["Data"];
Response.ContentType = dr["Type"].ToString();
这两行有错误,提示超出确保列表中的最大索引小于列表的大小,确保数据列名正确,确保索引不是负数,请高人指点下上面的代码到底错在哪里?
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
如楼上所说!
看看Pictures表中是否有Data和Type字段.或者是你把相应的字段拼写错了.
byte[] Data = new byte[(byte[])dr["Data"].Length];
Data = [(byte[])dr["Data"];我个人认为:
byte[] Data = null;
Data = (byte[])dr["Data"];
并不会给Data定义正确的长度,所以应该在定义数组时就确定好数组长度,以免超出索引