|
||||||
How to Connect to a MySQL Database with C#Preparing a C# Application for Running SQL QueriesUsing databases may seem daunting to the new C# programmer, but it shouldn't. Not if they're using the MySQL .NET connector.
Databases are an integral part of many C# applications. They are used to store data that can be used in any desktop or web based program. For example the database may contain:
Of course, these are only useful if the C# application can access the data. Fortunately that's not a problem if the information is stored in a MySQL database, and that's because MySQL provide a .NET connector. If a programmer uses the MySQL .NET connector then any of their C# applications (or, in fact, any of their .NET based applications) can connect to a MySQL database with very little effort. Downloading and Installing the MySQL .NET ConnectorThe MySQL .NET connector installer can be downloaded from the MySQL Connector/Net web page. Once that's been done then the installer can be run (by double clicking on it in Windows Explorer). When the installer has finished its task then the connector will be ready for use. Adding the MySQL .NET Connector as a Project ReferenceThe programmer must next add the MySQL .NET Connector as a project reference before they can use it in an IDE (Integrated Design Environment), such as SharpDevelop, where they do can this by:
They will now be able to use the connector in their application. Loading the MySQL Connection LibraryWith MySQL.Data set up as a reference the correct library will need to be loaded. In this case that is the MySqlClient library: using MySql.Data.MySqlClient;
The MySQL connector can now be used in the application. Creating the MySQL Database Connection Object with C#Now the programmer can create the connection object itself: MySqlConnection connection = new MySqlConnection ();
This new connection cannot be used yet. For that to happen the connection must be given a connection string. Setting the Connection StringThe connection string needs to contain all of the information required to connect to a database. These are:
Each of the elements must have a semicolon between them. For example: connection.ConnectionString =
"server=localhost;"
+ "database=aec;"
+ "uid=aec_user;"
+ "password=aec;";
Here the connection will be made on the localhost to the aec database via the aec_user account. Opening the MySQL Database ConnectionThe connection has not actually been made yet, but that is a very simple step: connection.Open ();
And now the connection is ready to receive SQL (Structured Query Language) statements such as:
This enables the programmer to carry out all of the database operations that they need. Closing the MySQL Database ConnectionThe database connection should close when the user exits from the application. However, it is always good practice to close the connection neatly in the code: connection.Close();
This will ensure that any memory uses by the connection will be freed up, and will stop the application from hogging too many of the computer’s resources. SummaryAny C# programmer wishing to connect to a MySQL database simply has to:
And they can then produce an application that connects seamlessly with any MySQL database.
The copyright of the article How to Connect to a MySQL Database with C# in C Programming is owned by Mark Alexander Bain. Permission to republish How to Connect to a MySQL Database with C# in print or online must be granted by the author in writing.
Comments
Jun 30, 2009 12:56 AM
Guest :
1 Comment:
|
||||||
|
|
||||||
|
|
||||||