数据读取器的空检查有什么问题

发布于 2024-08-29 04:49:50 字数 696 浏览 4 评论 0原文

            c.Open()
            r = x.ExecuteReader
            If Not r("filename").IsDbnull Then
                imagepath = "<img src='images/'" & getimage(r("filename")) & " border='0' align='absmiddle'"

            End If
            c.Close()
            r.Close()

我也尝试过;

If r("filename") Is DBNull.Value Then
            imagepath = String.Empty
        Else
            imagepath = "<img src='images/'" & getimage(r("filename")) & " border='0' align='absmiddle'"
        End If
        c.Close()
        r.Close()

错误是:不存在数据时尝试读取无效。

我的代码的想法是仅在数据可用时构建 img src 字符串。

非常感谢帮助。

谢谢

            c.Open()
            r = x.ExecuteReader
            If Not r("filename").IsDbnull Then
                imagepath = "<img src='images/'" & getimage(r("filename")) & " border='0' align='absmiddle'"

            End If
            c.Close()
            r.Close()

I have also tried;

If r("filename") Is DBNull.Value Then
            imagepath = String.Empty
        Else
            imagepath = "<img src='images/'" & getimage(r("filename")) & " border='0' align='absmiddle'"
        End If
        c.Close()
        r.Close()

The error is: Invalid attempt to read when no data is present.

The idea of my code is to build an img src string only when data is available.

Help greatly appreciated.

Thanks

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

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

发布评论

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

评论(3

剧终人散尽 2024-09-05 04:49:50

您需要调用 Read 方法。

r = x.ExecuteReader
r.Read()

You need to call the Read method on your SqlDataReader before data is available for reading.

r = x.ExecuteReader
r.Read()
谜泪 2024-09-05 04:49:50

需要首先调用 Read 方法。

If r.Read() AndAlso Not r("filename").IsDbnull Then ...

The Read method needs to be called first.

If r.Read() AndAlso Not r("filename").IsDbnull Then ...
乜一 2024-09-05 04:49:50

如果运行查询后 DataReader 中有 0 行,则根本没有字段,因此您无法将它们与 null 进行比较。

您可以使用 if r.HasRows then //got data ... 来检查这一点

If there are 0 rows in the DataReader after the query is run, there will be no fields at all so you can't compare them to null.

You can check for this with if r.HasRows then //got data ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文