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();
这两行有错误,提示超出确保列表中的最大索引小于列表的大小,确保数据列名正确,确保索引不是负数,请高人指点下上面的代码到底错在哪里?

解决方案 »

  1.   

    有没有Data这个字段
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  2.   

    有没有Data这个字段
    如楼上所说!
      

  3.   

    Data是什么东西 你没定义呢
      

  4.   

    "确保数据列名正确,确保索引不是负数".
    看看Pictures表中是否有Data和Type字段.或者是你把相应的字段拼写错了.
      

  5.   

    我遇到过和楼主一样的情况,我是用这种办法解决的,楼主你可以试试:
    byte[] Data = new byte[(byte[])dr["Data"].Length];
    Data = [(byte[])dr["Data"];我个人认为:
    byte[] Data = null;
    Data = (byte[])dr["Data"];
    并不会给Data定义正确的长度,所以应该在定义数组时就确定好数组长度,以免超出索引