Click here to Skip to main content
Licence 
First Posted 19 Feb 2004
Views 176,410
Bookmarked 47 times

Connecting to MySQL database from your .NET applications.

By | 19 Feb 2004 | Article
A simple introduction on how to connect to a MySQL database.
 
Part of The SQL Zone sponsored by
See Also

Introduction

The article tries to explain two possible ways to connect to a MySQL database. There are a lot of other ways through which you could connect to the MySQL database. Two among those are only discussed here.

Using the code

Before using the code, it is assumed that the MySQL database is existing in the system and is up and running. Then create a System DSN and give it the name which you give in the code. Also, make sure that you use the same UID and password. The below code connects to the MySQL database using the third party (Open Source) bridge ByteFX. This works the same way as the SqlConnection. For more information on the operations, please refer to the operation manual.

 ByteFX.Data.MySqlClient; 

...
...
...

//Now try to connect using ByteFX.
//This can be downloaded from http://www.bytefx.com/download.ashx?id=8
string myConnectionString ="DSN=MySQL;UID=root;PWD=root"; 
MySqlConnection myConnection = new MySqlConnection(myConnectionString);
string myInsertQuery = "Select * from Authors";
MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
MySqlDataReader objReader = myCommand.ExecuteReader(); 
int nResultCount = 0;
while (objReader.Read())
{
    ++nResultCount;
}

Another way to connect to the database is through the Microsoft ODBC.NET which is downloadable from Microsoft. The same pre requisites for the above code applies here.

using Microsoft.Data.Odbc;
...
...

//Connect to the database using the system DSN
//Download odbc.net from 
//http://www.microsoft.com/downloads/details.aspx?
//    FamilyId=6CCD8427-1017-4F33-A062-D165078E32B1&displaylang=en

string myConnectionString ="DSN=MySQL;UID=root;PWD=root"; 
OdbcConnection MyConn;
OdbcCommand MyCmd = new OdbcCommand();
MyConn = new OdbcConnection(myConnectionString);
MyConn.Open();
MyCmd.Connection = MyConn;
StringBuilder SQL = new StringBuilder();
SQL.Append("SELECT ");
SQL.Append("*");
SQL.Append("FROM ");
SQL.Append("Authors");
MyCmd.CommandText = SQL.ToString();
OdbcDataReader result = MyCmd.ExecuteReader(CommandBehavior.CloseConnection);
int nResultCount = 0;
while (result.Read())
{
    ++nResultCount;
}

Points of Interest

You could also possibly connect to MySQL database using the OleDB classes. But it gives out exceptions when trying to access it. The work around for these kind of exceptions and other problems can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sabith.V.Mannadiar

Program Manager

Singapore Singapore

Member

Started programming in C / C++ . Currently works in C# server side programming.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberbluehenn10:46 7 Jul '09  
GeneralThank u Pinmemberjasnavp22:29 11 Feb '08  
Generalsimple is nice ! Pinmemberlam do thanh1:36 23 May '06  
Jokecomplete the code and will work Pinmembersevenverse0:12 10 Feb '06  
GeneralThere are many alternatives. PinmemberHackPrince10:21 30 Jun '05  
GeneralMySQL Connector/Net PinmemberDead Skin Mask4:22 12 May '05  
GeneralThat was not bad PinsussAnonymous10:50 2 Nov '04  
GeneralThis is a sh*t!! PinmemberTasiek10:25 17 Oct '04  
GeneralRe: This is a sh*t!! Pinmemberflcatalin9:20 18 Oct '04  
GeneralRe: This is a sh*t!! PinsussAnonymous4:14 4 Aug '05  
GeneralConnecting to MySQL database from your .NET applications Pinmembervelkropie3:24 25 Aug '04  
Generalcan't use namespace PinmemberLocura6663:23 20 Jun '04  
GeneralRe: can't use namespace Pinmemberflcatalin9:13 18 Oct '04  
GeneralExecution error Pinmemberkamarul hairi23:36 26 Apr '04  
GeneralRe: Execution error PinmemberAbdul (Rajib) Bahar5:17 17 May '04  
Generaldownload error Pinmemberansiboy13:50 24 Feb '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Feb 2004
Article Copyright 2004 by Sabith.V.Mannadiar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid