A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Connecting to MySQL with C# and ODBC
In this tutorial we will see how a .NET application can connect to a MySQL server and perform queries on the database. The application uses the MyODBC driver to establish an ODBC connection to the server.
On Sunday, August 14th 2005 at 06:00 PM
By Andrew Pociu (View Profile)
(Rated 4.7 with 75 votes)
Setting up the MyODBC driver
Before starting you need to download and install MyODBC on the machine that will act as the client (the machine that will connect to the server). Here is what happens if you run the application and try to connect without having MyODBC installed and configured:
Download MyODBC 3.51 or download from MySQL.com
After installing, open Data Sources (ODBC) from the Control Panel. Click Add... and the following window shows up:
Fill the TextBoxes with information about your MySQL server. The important fields are Server, User and Password. Click Test and with a little bit of luck, you will see the following message:
You can leave the database field as it is, or you can choose a default database which the driver will connect to. Also, you don't need to deal with the other tabs of the window.
After clicking Ok to save the information, you will now see our newly created Data Source in the list:
Now we're ready to build the application and run it.
Creating a .NET application that connects to a MySQL database
Download the project
Note that the code is written using .NET 2.0 and the project is created in Visual Studio 2005. However, this won't stop you from using the code inside Visual Studio 2003 with .NET 1.1, because the code doesn't need any changes between versions of the .NET Framework.
Let's look at the most important pieces in our code.
Here we declared an ODBC connection, command and data reader. We will need all these to connect, query and read data from the MySQL Server. The ConStr variable is a normal string variable that holds the connection string, of course, we could have passed the connection string directly without using an additional variable, but the code is more organized this way.
Now let's have a look in the click event of the Connect button:
The connection string is initialized here, using data from the TextBoxes on the form. Note the version of the MySQL ODBC driver, inside. Carefully enclosing in try/catch blocks, the lines that open the connection, we change the connection state of the ODBC connection to Open. We first check to make sure the connection is not already open.
Now to list the tables inside the database:
We first check the state of the connection. If it is closed, we don't want to try to execute a command on the MySQL Server. Then we execute the SHOW TABLES query, which is the MySQL query for getting a list of tables in the selected database. Using a DataReader we pass through each table and display it inside the txtLog TextBox.
To keep this tutorial simple, I haven't added any other commands to be executed on the MySQL server, however you can perform any operation on the server. You can create new databases, select values, create tables, add values, edit values, delete databases and any other operation that you can think of, that MySQL supports through a query.
Also I should mention that even though this is a Windows application, the ODBC connection method works great with web applications too. That means you can easily build ASP.NET applications that use a MySQL database.
Here is the application running on Windows Vista:
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search
From the creators of Geekpedia, a revolutionary new coupon website!
BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.