VB.Net & SQL connection

cuemasterfl

Supreme [H]ardness
Joined
Jul 5, 2001
Messages
4,181
I'm trying to make my VB program connect to a MySQL database and retrieve some info from a query, and then have the result of that query show in a message box. I always get a 'blank' message box. I know the connection is being made, but obviously I'm doing something wrong. Any help would be appreciated. Thanks.

Edit: the database is on a remote server (not local)

Code:
            Dim MysqlConn As MySqlConnection
            Dim SQLCommand As New MySqlCommand
            Dim SQLAdapter As New MySqlDataAdapter
            Dim SQLData As New DataTable
            Dim SQL As String

            MysqlConn = New MySqlConnection()

            ' Define the SQL to grab data from table.
            SQL = "select * from users"

            'Connection String
            MysqlConn.ConnectionString = "server=xxx.xxx.xxx.xxx;" & "user id=xxxxx;" & "password=xxxxx;" & "database=xxxxx"

            ' Try, Catch, Finally
            Try
                MysqlConn.Open()

                SQLCommand.Connection = MysqlConn
                SQLCommand.CommandText = SQL

                SQLAdapter.SelectCommand = SQLCommand
                Dim result As String = SQLAdapter.Fill(SQLData)

                MsgBox(result)

            Catch myerror As MySqlException
                MessageBox.Show("Cannot connect to database: " & myerror.Message)
            Finally
                MysqlConn.Close()
                MysqlConn.Dispose()
            End Try
 
Its been 10 years since I've meaningfully tinkered with .NET/coding, but I thought you had to return database data into a data set, then iterate through it to hit the message box. You're basically stuffing a data adapter function result into a string... Don't you need to put it in a dataset first, then loop through the rows to populate the string being fed to the messagebox?
 
SQLAdapter.Fill() does not return the text, it populates the DataTable. You will need to read the table rows to get the results.

Something along the lines of:

Dim result As String
ForEach (DataRow row in SQLData)
'Will create a comma-delimited list of row data
result += row["UserName"] + ", " + row["UserID"] + ", " + row["col3"] + "\r\n"

MsgBox(result)
 
Back
Top