I’m having trouble with a VB.NET function that reads user credentials from a database. When I try to convert a binary field to string, I get weird results. The database column is stored as BINARY type, but when I retrieve it through my data reader, the string conversion doesn’t work properly.
Sub LoadUserCredentials()
Dim connectionStr As String = "server=" & serverName & ";database=" & dbName & ";uid=myuser;pwd=mypass;"
Using conn As New SqlConnection(connectionStr)
conn.Open()
Dim query As String = "SELECT loginname, CAST(CONVERT(varchar, loginpass) AS varchar) AS 'loginpass' FROM accounts WHERE role='Administrator'"
Using cmd As New SqlCommand(query, conn)
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Dim userName As String = reader("loginname").ToString()
Dim userPass As String = reader("loginpass").ToString() ' This line causes issues
End While
End Using
End Using
End Using
End Sub
The problem is that when I convert the binary password field to string, it shows up with missing characters or strange formatting. The output looks incomplete compared to what should be there. Has anyone dealt with binary to string conversion issues in SQL Server before?