Jump to content


Photo
- - - - -

Using .NET & MySQL Basis


  • Please log in to reply
2 replies to this topic

#1 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 28 June 2008 - 12:43 AM

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:

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.

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.

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.

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. :)

Edited by JoeyMagz, 26 July 2008 - 11:45 PM.


#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 June 2008 - 11:49 AM

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 :))

#3 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 28 June 2008 - 12:29 PM

oops...when i posted that it was 2am...lol




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users