在 VB.Net 中将 SQLDataReader 转换为字符串

发布于 2024-09-29 07:50:54 字数 456 浏览 0 评论 0原文

我正在从数据库返回一行,并且我想将 SQLDataReader 转换为字符串格式,以便我可以将其传递给我的 Web 服务。

        Dim rdr As SqlDataReader = sqlcmd.ExecuteReader

        If rdr.HasRows Then
            rdr.Read()
            GetInvHeaderValue = Convert.ToString(rdr.Read())
            Return GetInvHeaderValue
        Else
            GetInvHeaderValue = "<ERR>No Records Returned</ERR>"
        End If

如何将 SQLDataReader 转换为字符串?

有更好的选择吗?

I am returning one row from the database, and I want to convert the SQLDataReader to a string format, so I can pass it to my webservice.

        Dim rdr As SqlDataReader = sqlcmd.ExecuteReader

        If rdr.HasRows Then
            rdr.Read()
            GetInvHeaderValue = Convert.ToString(rdr.Read())
            Return GetInvHeaderValue
        Else
            GetInvHeaderValue = "<ERR>No Records Returned</ERR>"
        End If

How would I convert a SQLDataReader to a string?

Is there a better alternative?

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

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

发布评论

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

评论(1

静若繁花 2024-10-06 07:50:54

rdr.Read() 将 DataReader 移动到下一条记录,如果存在下一条记录则返回。所以你可以这样写:

Dim GetInvHeaderValue As Object
While rdr.Read()
   GetInvHeaderValue  = rdr(0)'if this value is in Column-Index 0'
   GetInvHeaderValue  = rdr("GetInvHeaderValue")'if a Column with this name exists'
   GetInvHeaderValue  = rdr.GetString(0)'returns a String representation(there are getter for all common types)'
End While

你只是将指示是否有下一条记录的布尔值转换为字符串(“True”/“False”)。

请参阅 MSDN 了解更多信息。

rdr.Read() moves the DataReader to the next records and returns if there is a next record at all. So you can write:

Dim GetInvHeaderValue As Object
While rdr.Read()
   GetInvHeaderValue  = rdr(0)'if this value is in Column-Index 0'
   GetInvHeaderValue  = rdr("GetInvHeaderValue")'if a Column with this name exists'
   GetInvHeaderValue  = rdr.GetString(0)'returns a String representation(there are getter for all common types)'
End While

You are only converting the Boolean that indicates if there is a next record to a String("True"/"False").

Have a look at MSDN for further onformations.

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