Getting MySQL and Visual
Studio.NET Working Together |
||||||||||||||||||||||||||||||||
Introduction - What's in This Guide Getting Started -
Downloading What You'll Need
Alright. As you can see, we'll be using MySQL 4.0, MDAC 2.7, and one of the many listed MyOLEDB drivers listed on MySQL's website. I'm not using VS.NET 2003, so if you have that product I cannot ensure that this guide will work (or is even necessary). If you are going to use phpMyAdmin for setting up and working with the database, you'll also need a web server (such as Apache), and obviously PHP so you can run phpMyAdmin. You don't have to have phpMyAdmin as you can do everything with a command-line tool provided with MySQL, but it makes the process a lot nicer. If you've already got all of what you need installed (MyOLEDB Driver, MySQL server, and a database/know how to edit it) then just skip all of this following information and go to the "Ok, Now I've Done All of That Crap--What About Visual Studio?" section, seven section below. Installing a MySQL
Server 1) Install the Server. Installing the MyOLEDB
Driver Optional #1 - Installing
Apache 2.0 Optional #2 - Installing
PHP 4.3.2 Optional #3 - Installing
phpMyAdmin
*(Use the password that you used for your MySQL server. If you skipped that part, then you might also need to change the username accordingly. The purpose is to tell it what username and password to use to establish a connection to the database.) Now open up the httpd.conf file again and add
this information: Once again, you'll need to change the C:/Web/phpadmin2 directory to where you put phpMyAdmin. Yes, use forward slashes. Use them anywhere I use forward slashes. Some people are just backward... go figure. If the AuthUserFile path is incorrect (to your Apache directory) please change it accordingly. Leave the /passes/passwords after it though. For example if you installed Apache to W:\Apache instead, you might change it to W:/Apache/Apache2/passes/passwords. Also be sure to change WhateverUserNameYouWant to a username of your choice. This doesn't have to be the same username as your MySQL username--in fact I discourage it. Now open up a command prompt (Start>Run; type cmd
and press enter). Navigate to your
Apache2\bin\ directory and type the following
command without quotes: Make sure to change YourApacheDirectory to your installation directory and TheUserNameYouUsedAbove to the same (including case) username you supplied for WhateverUserNameYouWant. If it errors, make sure the Apache2\passes\ directory exists. If not, then create it. If it didn't error, you should be all setup. It will ask you for a password and to confirm that password. After that, it should be all green lights. Make sure to restart the Apache2 service once again. Now your phpMyAdmin directory should be password protected against intruders. Open up a browser window and go to http://localhost/phpmyadmin (or whatever you named it). It should prompt you for a username and a password (which Windows can save for you while still keeping prying eyes out). I suggest you bookmark it. I've Had Problems With
One of Those Steps (If you had no problems, skip to
the next section) Ok, Now I've Done All of
That Crap--What About Visual Studio? Now, assuming you've installed the MyOLEDB driver and your MySQL server is up and running, let's begin. I only cover VB.NET code, but the methodology is similar for C# as well. If you're programming and have come this far using C++, then you should be able to adapt this to your language. Dim fdCon As New OleDb.OleDbConnection("Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")Dim fdCom As New OleDb.OleDbCommand("SELECT * FROM TABLENAME ORDER BY DESIREDFIELD ASC", fdCon) fdCom.Connection.Open() Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(CommandBehavior.CloseConnection) While
fdRead.Read You will need to change
the bold parts to the appropriate information.
For example, DATABASE should be the name of your
MySQL database, YOURSQLUSERNAME is the username
(probably root) for the database, and the same
concept for YOURSQLPASSWORD. Change TABLENAME
to the name of your table. It's a good idea to
stay away from all capital names in SQL. You
don't have to use the ORDER BY DESIRED FIELD ASC
statement, but you can to sort the data. If
you want to use it, change the DESIREDFIELD to the
name of one of your fields. Sorting by an ID
field if you've got one is always a good idea.
ASC = ascending; DESC = descending. For more
information on SQL commands, please visit a site
such as: Now that you've learned basic data retrieval, let's go over a non-query. Dim fdCon As New OleDb.OleDbConnection("Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")Dim fdCom As New OleDb.OleDbCommand("", fdCon) fdCom.Connection.Open() Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(CommandBehavior.CloseConnection) fdCom.CommandText = "INSERT INTO TABLENAME
(FIELDNAME1, FIELDNAME2) VALUES (""lookatme"", ""newline!"")" fdCon.Close() Once again, replace TABLENAME and the FIELDNAMEx to appropriate values. You can have more or less fields (depending on what's in your table) or select one the fields you want. However, take notice of the order in which you list them as that's the order in which the VALUES will be placed. If you take notice, "lookatme" would be inserted as FIELDNAME1 and "newline!" would be inserted for FIELDNAME2. Using a double double-quote in VB acts as an escape character and actually inserts a real double-quote. It's always a good practice to either use that or a solitary single-quote around your variable names. If you were going to use the variables ImaVar1 and ImaVar2 with double-quotes, you could do it like this: Dim fdCon As New OleDb.OleDbConnection("Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")Dim fdCom As New OleDb.OleDbCommand("", fdCon) fdCom.Connection.Open() Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(CommandBehavior.CloseConnection) fdCom.CommandText = "INSERT INTO TABLENAME
(FIELDNAME1, FIELDNAME2) VALUES (""" & ImaVar1 &
""", """ & ImaVar2 & """)" fdCon.Close() Yes, that's three double-quotes together. That's the double double-quote inside of a quoted string. It stores an actual quote there. Now, to use single quotes you might do it like below: Dim fdCon As New OleDb.OleDbConnection("Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")Dim fdCom As New OleDb.OleDbCommand("", fdCon) fdCom.Connection.Open() Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(CommandBehavior.CloseConnection) fdCom.CommandText = "INSERT INTO TABLENAME
(FIELDNAME1, FIELDNAME2) VALUES ('" & ImaVar1 & "',
'" & ImaVar2 & "')" fdCon.Close() That's a single-quote on the inside of the string declaration quotes. ' " and " ' accordingly--no spaces between them. I guess that about wraps it up. If you need help on something, fire off an email to mysqlhelp@pcrpg.org and I'll get back to you as soon as I can. Thanks for reading this tutorial--it's pretty long, I know. I could have spent a Saturday better ways, trust me!
|