开发者

Converting SQLDataReader to String in VB.Net

开发者 https://www.devze.com 2023-01-22 18:50 出处:网络
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.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消