使用 C# 从数据库读取图像?

发布于 2024-07-23 04:58:04 字数 847 浏览 2 评论 0原文

我有这个查询:(

SELECT PICTURE FROM LOGO WHERE LOGONO = ?

“PICTURE”是 SQL Server 中的图像列)

并且读取数据的代码:

using (DbDataReader reader = command.ExecuteReader())
{
    if (reader.HasRows)
    {
        if (!reader.IsDBNull(0))
        {
            byte[] buffer = new byte[8000];
            while (reader.GetBytes(0, 0, buffer, 0, 8000) > 0)
                picture.AddRange(buffer);
        }
     }
}

问题是 InvalidOperationException(“行/列不存在数据”)总是被抛出到读取器上.IsDBNull(0) 语句,当列包含 null 时。

MSDN 说:

在调用类型化 get 方法(例如 GetByte、GetChar 等)之前调用此方法来查看是否存在空列值,以避免引发错误。

如何检查该列不包含 null 而不触发异常? 我是否以错误的方式处理这件事?

I've got this query:

SELECT PICTURE FROM LOGO WHERE LOGONO = ?

("PICTURE" is an Image column in SQL Server)

And this code to read the data:

using (DbDataReader reader = command.ExecuteReader())
{
    if (reader.HasRows)
    {
        if (!reader.IsDBNull(0))
        {
            byte[] buffer = new byte[8000];
            while (reader.GetBytes(0, 0, buffer, 0, 8000) > 0)
                picture.AddRange(buffer);
        }
     }
}

The problem is an InvalidOperationException ("No data exists for the row/column") always gets thrown on the reader.IsDBNull(0) statement when the column contains null.

MSDN says:

Call this method to see if there are null column values before calling the typed get methods (for example, GetByte, GetChar, and so on) to avoid raising an error.

How can I check that the column doesn't contain null without triggering the Exception? Am I going about this in the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海未深 2024-07-30 04:58:04

您需要调用 reader。Read() 在访问任何数据之前。 从 MSDN 文档中阅读:

DataTableReader 的默认位置是在第一条记录之前。 因此,您必须调用 Read 才能开始访问任何数据。

You need to call reader.Read() before accessing any data. From the MSDN docs for Read:

The default position of the DataTableReader is before the first record. Therefore, you must call Read to start accessing any data.

时光匆匆的小流年 2024-07-30 04:58:04

您没有调用 reader.Read()。 由于您使用读取器,因此您要么需要检查 Read 是否对单个记录返回 true,要么使用 while 循环来迭代所有记录。

if (reader.Read())
{
  // handle single record
}

// or

while (reader.Read())
{
  // handle each record
}

You did not call reader.Read(). Since your working with a reader, you either need to check if Read returns true for a single record, or use a while loop to iterate through all the records.

if (reader.Read())
{
  // handle single record
}

// or

while (reader.Read())
{
  // handle each record
}
[旋木] 2024-07-30 04:58:04

不是更好吗……

if (!reader.IsDBNull(#))
{...}

或者也许是简短的版本……

reader.IsDBNull(#) ? [default] : [value];

???

Isn't better...

if (!reader.IsDBNull(#))
{...}

or perhaps the short version...

reader.IsDBNull(#) ? [default] : [value];

???

深府石板幽径 2024-07-30 04:58:04

我总是使用以下检查,它似乎一直对我有用。

if (reader[0] != null && reader[0] != DBNull.Value)
{
}

I always use the following check and it seems to have always worked for me.

if (reader[0] != null && reader[0] != DBNull.Value)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文