C# 在sql server中检索varbinary映像(当它不为null时)

C# 在sql server中检索varbinary映像(当它不为null时),c#,sql-server,sql-server-2008,varbinarymax,C#,Sql Server,Sql Server 2008,Varbinarymax,我正在尝试从SQL Server 2008数据库中检索存储为varbinary(max)列的图像。当image列不等于NULL时,我从数据库中获取图像。下面是我用来检索图像的代码。请帮我看一下我的代码,我不知道我哪里做错了。谢谢 protected void Page_Load(object sender, EventArgs e) { string strQuery = "select profilepicture from MemberAccount where nric='"+ S

我正在尝试从SQL Server 2008数据库中检索存储为
varbinary(max)
列的图像。当image列不等于
NULL
时,我从数据库中获取图像。下面是我用来检索图像的代码。请帮我看一下我的代码,我不知道我哪里做错了。谢谢

protected void Page_Load(object sender, EventArgs e)
{
    string strQuery = "select profilepicture from MemberAccount where nric='"+ Session["nric"] +"' and profilepicture IS NOT NULL";
    SqlCommand cmd = new SqlCommand(strQuery);
    DataTable dt = GetData(cmd);
    if (dt != null)
    {
       download(dt);
    }
}

private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;

    try
    {
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
    catch
    {
        return null;
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
    }
}

private void download(DataTable dt)
{
    Byte[] bytes = (Byte[])dt.Rows[0]["profilepicture"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpg";
    Response.BinaryWrite(bytes);
    Response.End();
}
行:

错误:

位置0处没有行


防御性数据库编程101:您需要检查是否获得数据!无法将任何内容转换为图像。。。你不能盲目地假设你会得到数据。总是检查

private void download(DataTable dt)
{
    // check if you have any rows at all 
    // no rows -> no data to convert!
    if(dt.Rows.Count <= 0)
       return;

    // check if your row #0 even contains data -> if not, you can't do anything!
    if(data.Rows[0].IsNull("profilepicture"))
       return;

    Byte[] bytes = (Byte[])dt.Rows[0]["profilepicture"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpg";
    Response.BinaryWrite(bytes);
    Response.End();
}
private void下载(数据表dt)
{
//检查是否有任何行
//无行->无需转换的数据!
如果(dt.Rows.Count)如果没有,你就什么也做不了!
if(data.Rows[0].IsNull(“profilepicture”))
返回;
字节[]字节=(字节[])dt.Rows[0][“profilepicture”];
Response.Buffer=true;
响应。Charset=“”;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType=“image/jpg”;
响应。二进制写入(字节);
Response.End();
}

了解如何使用CommandParameters以及会话[“nric”]中的值是什么。如果使用该值直接在SSMS中执行查询会发生什么?您可以添加额外的保护措施:if(dt!=null&&dt.Rows!=null&&dt.Rows.Count>0)有些离题,但仍然……@Rene:如果dt不为null,那么dt.Rows将永远不会为null,因为这是在DataTable的构造函数中初始化的,所以您可以将代码缩短为(dt!=null&&dt.Rows.Count>0)@ManishDalal,这是真的。我没有检查实现细节。。。
private void download(DataTable dt)
{
    // check if you have any rows at all 
    // no rows -> no data to convert!
    if(dt.Rows.Count <= 0)
       return;

    // check if your row #0 even contains data -> if not, you can't do anything!
    if(data.Rows[0].IsNull("profilepicture"))
       return;

    Byte[] bytes = (Byte[])dt.Rows[0]["profilepicture"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpg";
    Response.BinaryWrite(bytes);
    Response.End();
}