Hello All,
I'm fairly new to MYSQL and VB.net and i'm trying to master the concept. I'd appreciate any help that could be provided.
I have developed the following code which should connect to a MYSQL server and then take from it every value in a column called 'Username'. When running, it connects to the database and then disconnects.
What am I doing wrong? I know that the actual connection is okay so it must be something to do with the way in which I am trying to retrieve data. Perhaps I have misunderstood a fundamental concept here?
Code:'initialise appropriate variables Dim conn As MySqlConnection Dim sqlreader As MySqlDataReader Dim sqlcomm As New MySqlCommand("SELECT Username, Password FROM user_data", conn) Try 'attempt connection conn = New MySqlConnection() 'set connection string conn.ConnectionString = "server=server.com" & ";" _ & "user id=" & "userid" & ";" _ & "password=" & "password" & ";" _ & "database=databasename" 'attempt to open connection conn.Open() 'notify of successful connection MsgBox("Successfully connected to database server.") Using sqlreader As MySqlDataReader = sqlcomm.ExecuteReader() While sqlreader.Read() 'Iterate through usernames MsgBox(sqlreader("Username")) End While End Using 'if connection is not successful then catch Catch 'notify of unsuccessful connection MsgBox("There was an error connecting to the database server.") 'close connection conn.Close() End Try




Reply With Quote