Quick Search for:  in language:    
VSNET,youre,just,wondering,MySQL,with,having,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 140,189. lines
 Jobs: 376. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for .Net.
Click here to see a screenshot of this code!Ray Picking for 3d Objects
By Jay1_z on 11/23

(Screen Shot)

Embed Images into Xml Files
By Chris Richards on 11/23


Encryption and Alternate Data Stream
By Philip Pierce on 11/23


Click here to see a screenshot of this code!AddressBook
By Ekong George Ekong on 11/23

(Screen Shot)

Click here to see a screenshot of this code!Command Line Redirection
By kaze on 11/23

(Screen Shot)

Fader
By Ahmad Hammad on 11/23


Click here to see a screenshot of this code!Get content of a web page (simple)
By Tin Trung Dang on 11/22

(Screen Shot)

Retrieve the long path name for a short path name.
By Özgür Aytekin on 11/22


Easy Randomizer
By Christian Müller on 11/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Using MySQL with Visual Studio

Print
Email
 

Submitted on: 6/14/2003 4:01:18 PM
By: Particle 
Level: Intermediate
User Rating: By 9 Users
Compatibility:VB.NET

Users have accessed this article 9236 times.
 
 
     If you're just wondering how to use MySQL with VS.NET or are having problems, give this a try.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Introduction
     Hello, my name's Geoff (aka Particle) and if you're here you're probably wondering how to get MySQL to work with Visual Studio.NET.  I had the same problem this morning and it took hours to find everything I needed.  Therefore, I thought I'd make a guide to save other people time.  If you're completely new to MySQL, please visit www.pcrpg.org/TRPGguides/mysqloledotnet.php for complete instructions on how to install and configure MySQL, useful tools such as phpMyAdmin, and everything needed to make phpMyAdmin work.  What I am posting here is just the last portion of the guide dealing with VS.NET itself.  I'll leave the "What You'll Need" table from the beginning of the guide in tact (except for the web components and installing MySQL, etc--for those please visit the link above).  Some of the links have been broken into two lines.  Sorry about the links--it appears that due to some immaturity, hyperlinks can no longer be used so I'll just bold them.
 
MyOLEDB Driver http://www.mysql.com/downloads/download.php?file=Downloads/
Win32/MyOLEDB3.exe&pick=mirror
Microsoft Visual Studio .NET http://msdn.microsoft.com/vstudio/
   
Other Stuff That's Nice to Have:  
.NET Framework 1.1 http://www.microsoft.com/downloads/details.aspx?FamilyID=
262d25e3-f589-4842-8157-034d1e7cf3a3&DisplayLang=en
.NET Framework SDK 1.1 http://www.microsoft.com/downloads/details.aspx?FamilyId=
9B3A2CA6-3647-4070-9F41-A333C6B9181D&displaylang=en
MDAC v2.7 http://www.microsoft.com/downloads/details.aspx?FamilyID=
9ad000f2-cae7-493d-b0f3-ae36c570ade8&DisplayLang=en

     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 were getting a message along the lines of "MySQLProv was not registered on localhost" then installing the MyOLEDB driver I linked to should fix that problem.

Ok, Now I've Done All of That Crap--What About Visual Studio?
     If you have a database setup, you can proceed to work in Visual Studio.  If not, use phpMyAdmin to create a database and a simple table.  Usage of phpMyAdmin is rather straight-forward.  If you need help, try the documentation for it above.  Also, there are links named "Documentation" everywhere throughout phpMyAdmin that can point you to help.  (Information on using phpMyAdmin is available at www.pcrpg.org/TRPGguides/mysqloledotnet.php)

     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
     'Do what you want here.  Below is code that will pop up a message box for every record.
     'This code would work if your table had three fields.
     'This database's first field is an auto-incrementing ID medium integer, second field is a
     'VarChar, and the third is also a VarChar.  This code displays each field on its own line.

     MsgBox(fdRead.GetValue(0) & vbCrLf & fdRead.GetValue(1) & vbCrLf & fdRead.GetValue(2))

End
While

fdCon.Close()

     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:
http://www.phpfreaks.com/postgresqlmanual/page/sql-commands.html
or for an explained easy-to-learn course of basic SQL commands such as INSERT, SELECT, UPDATE, and DELETE go to:
http://www.developerfusion.com/show/48/1/
Once you've learned the stuff at Developer Fusion, the PHP Freaks page will come in handy as you'll understand it better.

     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!"")"
fdCom.ExecuteNonQuery()


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 & """)"
fdCom.ExecuteNonQuery()


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 & "')"
fdCom.ExecuteNonQuery()


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!


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
6/14/2003 9:33:59 PM:Brian Clark
Well I have to say this is excellent! I use MySQL and PHP all the time and needed to make something in VB.NET similar to my PHP workings. Thanks and you get 5 globes from me :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/14/2003 10:13:59 PM:Particle
Hey, thanks. Five hours on PSC and I already have 5 "excellent" marks and feedback! Thanks for the support guys! (BTW, many say that OLE is faster than ODBC--even though it isn't supported officially by the guys at MySQL.)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/15/2003 8:47:58 AM:
Philip Pierce

great job. 5 globes
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2003 6:13:05 PM:
Exactly what i was looking for, excellent articl thanx!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2003 9:31:26 PM:
you could have just posted the connection string that wouldve saved time for busy people reading your looooong article :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2003 11:49:41 PM:Particle
Heh, true! There are some people that might be new to OLEDB's altogether though. Also, if you thought this was long check out the full tutorial at pcrpg.org (link at the start of this article). =D
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/1/2003 4:05:05 AM:Serkan GERAY
It's simple way. I think so . I am using this method also in VB 6.0
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/4/2003 1:17:22 PM:Joseph Huntley
There is alsoa MySQL ADO.NEt provider which can be used just like Sql or OleDb, but is made specifically for MySql. Two providers are available for download at http://www.mysql.com/downloads/api-dotne t.html
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/12/2003 10:06:16 AM:
You can also view MySQL tables in visual studio. Look at www.sevenobjects.com/mysql.aspx
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/11/2003 10:03:13 PM:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll Additional information: The 'MySQLProv' provider is not registered on the local machine. highlighted line: fdCom.Connection.Open() What's wrong? Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/11/2003 11:26:39 PM:Particle
You didn't install the MySQL Provider that I linked to. It is not optional--you have to install it for MySQLProv to work.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.