by
171 11
0
5
2,132
0
Top 1% !
Popular
Famous
Easy-to-find
Refined
Popularity: 9737th place
This snippet is Public
.NETFramework4.5
Architecturex86_64
Extension.vb
LanguageVB.NET
OSWindows
Windows VersionWindows8

Simple MySQL database connection with select statement

This snippet has coode to pull the rows from the db in a while loop.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/647948/Simple-MySQL-database-connection-with-se?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
'Import this to the very top of your code, make sure you have the mysql connector installed for VS Imports mysql.data.mysqlclient Dim conn As New MySqlConnection Dim cmd As New MySqlCommand Dim read As MySqlDataReader Try 'default root would have no pw...simply remove the password part if you have no pw conn.ConnectionString = "server=127.0.0.1;user id=root; password=yourpw; database=mydb" cmd.Connection = conn conn.Open() cmd.CommandText = "SELECT id, name FROM mydb.table" read = cmd.ExecuteReader If read.HasRows Then While read.Read 'name this variable anything you want Dim id = read.GetInt32("id") 'for getting a string value: Dim sName = read.GetString("name") 'make the pvname and pvid public(global) variables so you could use them outside while loop, 'this might not be needed in some cases pvname = sName pvid = id End While End If Catch ex As Exception MsgBox(ex.Message) End Try
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus