Help - Search - Members - Calendar
Full Version: Using .NET & MySQL Basis
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > Miscellaneous Tutorials
JoeyMagz
Now before I begin, this is not an actual tutorial, it's just something to help people out who want to know how to start using MySQL in their .NET applications. I'm going to post the code in VB.NET format which is EASILY converted to C#.

First you have to go to mysql.com and download the .NET Connector. Then add that into your application as a reference. Call it in the top of the code:

CODE
Imports MySql.Data.MySqlClient


As you can see the coding is very similar already. Then after that is where it gets hard. I personally create a class variable called myConnString which has the connection string set to it so I don't have to keep re-writing it over and over.

CODE
Dim myConnString = "server=YOURHOST;user id=YOURUSERNAME;password=YOURPASSWORD; database=YOURDB"


That wasn't too bad. Again the point of this is to just show you how to do stuff, it's up to you to take what I show you and turn it into your own. The next part is about inserting information into tables.

CODE
Dim command As New MySqlCommand
Dim conn As New MySqlConnection(myConnString) 'your connection string
command.CommandText = "INSERT INTO sometable(id, money, picture) VALUES('Someid','someval','someobject')"
Try
    conn.Open()
    command.ExecuteNonQuery() 'Executes a query the requires no output
Catch error As MySqlException
    MessageBox.Show("MySQL Error: " & error)
End Try


Again, simple. Now for selecting information from the database and putting it into variables which I found to be the hardest part but with a lot of trial and error found it out.

CODE
Dim command As New MySqlCommand
Dim read As New MySqlDataReader()
Dim conn As New MySqlConnection(myConnString) 'your connection string

conn.Open()
command.CommandText = "SELECT * FROM sometable WHERE username='" & txtUser.Text & "' AND password='" & txtPass.Text & "'"

read = command.ExecuteReader()

While read.Read()
    Dim id = read("id").ToString()
    Dim email = read("email").ToString()
End While


Now with that code above you would be able to display the id and e-mail address of the user with that username and password combo anywhere you wanted. You can now put that code anywhere on your form: textboxes, listboxes, etc.

Well, I hope that helped everyone out with any confusion about using MySQL and their .NET applications. Also, I'm on a work computer right now so I don't have visual basic or anything on here to test out the code. If it doesn't work, let me know and I'll fix it when I get home. Everything should work though. biggrin.gif
rc69
Might not be a tutorial, but as it is closer to a tutorial then a request for help, it does belong in the tutorial section (moved smile.gif)
JoeyMagz
oops...when i posted that it was 2am...lol
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.