.NET: Connecting To A MySQL Database

I recently started fooling around with .NET. I never had the urge to try it out before until now. I thought, since these days I got nothing to do in the meantime at work, I could study a new technology to add to my resume. I have been reading about .NET and using its IDE called Visual Studio. My phase is a bit slow since I’m fairly new to this area. Anyway, on to the topic of this post. Majority of web applications involves having interactions with the database. However, Visual Studio only includes connectors for its MS Sql Server to interact with the database. I think Oracle is also included. Another way to connect to a variety of databases is via ODBC but I don’t like using that. Since I want to use MySQL, I had no way to connect to it unless I find a connector for it specifically. Currently, the .NET framework is at version 2 so a MySQLConnector made specifically for that should be downloaded. You can get it here. If you happen to come across a MySQLConnector link for .NET 1.0, that will not work if your .NET framework is version 2.

This post assumes you know your way around Visual Studio and its coding syntax. If not, please review the basics of ASP.NET. Install the connector. Go to your Visual Studio and right click the project name in the Solution Explorer window. Right click, choose Add Reference and browse where the MySQL connector DLL file is located. To check if creating a connection is successful or not, we need to add some C# code. Make sure you include the namespace MySql.Data.MySqlClient by using the keyword using. Create a Label control and name the ID lblInfo. In the Solution Explorer, under the aspx file where the Label control is located, go to the .cs file that falls under it. This is the C# file that is associated with the .aspx file. You will find a method there named Page_Load(). inside the method, add the following code.

That’s it. Run your web application and see if the connection is successful or not.

Related Posts Plugin for WordPress, Blogger...

6 comments

Leave a Reply

Your email address will not be published. Required fields are marked *