Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Connecting to MySQL from Visual Basic

2010-03-06

If it is in your control, stay with SQL Server! If it isn’t, this is how you can do it.

When you install the .NET Connector for MySQL, a class library called MySql.Data is installed. You can add a reference to it from the Add Reference dialog. This gives you a new set of classes for accessing a MySQL database. This is what a connection to a database can look like:

Dim ConnectionString As String = "Server=XXX;Port=3306;Database=XXX;Uid=XXX;Pwd=XXX"
Using Cn As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString)
  Cn.Open()

  Console.WriteLine(Cn.State.ToString())

  Cn.Close()
End Using

If you know you ADO.NET, the rest should be a stroll in the park. This code lists keywords in MySQL:

Dim ConnectionString As String = "Server=XXX;Port=3306;Database=mysql;Uid=XXX;Pwd=XXX"
Using Cn As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString)
  Cn.Open()

  Using Cmd As New MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM help_keyword", Cn)
    Dim R As MySql.Data.MySqlClient.MySqlDataReader = Cmd.ExecuteReader()
    While R.Read()
      Console.WriteLine(R.GetString(1))
    End While
    R.Close()
  End Using

  Cn.Close()
End Using

Categories: Visual Basic 9

2 responses to “Connecting to MySQL from Visual Basic”

  1. […] posted here: Connecting to MySQL from Visual Basic – WinSoft.se Posted in Uncategorized | Tags: class-library, connector, from-the-add, net, new-set, […]

  2. Wiersz Poleceń Windows CMD…

    Windows XP Vosten SP3 PRO 2010 released now! this windows combining the beauty of appearance and the advanced performance, The System has been updated completely with the latest updates and hotfixes from Microsoft to maintain the highest level of secur…

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips