I am trying to read data from remote SQL base. I have written the function that works great if you need to get 1 Value , but when I want to read 2 or more value at from one query it doesn't work
how can I fix it?
Imports System.Data.SqlClient
Public Class SQL
Public MySQLconnection As SqlConnection
Public MySQLcommand As SqlCommand
Dim MySQLreader As SqlDataReader
Public Function connectToSQL(ByVal ConnectionString As String) As String
Try
MySQLconnection = New SqlConnection("Data Source=mysource;Initial Catalog=testbase;User ID=MYuser;password=MyPass")
MySQLconnection.Open()
MySQLcommand = New SqlCommand(ConnectionString, MySQLconnection)
If ConnectionState.Open Then
MySQLreader = MySQLcommand.ExecuteReader()
End If
Catch ex As SqlException
MsgBox("Connection Eror")
MsgBox(ex.Message)
End Try
Dim ReadData As String
Dim i As Integer = -1
While MySQLreader.Read
i += 1
ReadData += MySQLreader.GetValue(i).ToString
End While
Return ReadData
MySQLconnection.Close()
End Function
End Class
I am using this function like this:
Dim connectionstring As String
connectionstring = ("SELECT ID, username FROM [mybase].[myuser].[users] where username ='" & DataGridView1.SelectedCells(0).Value & "'")
Dim GetVaule As New SQL
MsgBox(GetVaule.connectToSQL(connectionstring))
I want to get from this query both ID and username - but I receive only ID
Please help me to fix it.