Knowledge Essentials - 3Essentials Hosting

Connecting to a MySQL DB with ASP.net

Article ID: 388

 Back to Search

Connecting to a MySQL database with ASP.net will vary depending on the .Net version you're running:

  • .Net 1.1:
    • To connect to a MySQL database with ASP.Net 1.x, you cannot use the OleDbConnection provider/class, or the SQLConnection provider/class (which is for MSSQL databases only), instead you need to use the .Net ODBC provider.  The .NET ODBC provider was not included in the .Net 1.x Framework, and thus must be added to the server seperately.  3Essentials is adding this to servers only on an as-requested basis.  If you intend to use .Net 1.1 to connecto a MySQL database, please submit a support request asking of the .Net ODBC provider has been installed on the server where your site is hosted. (if it isn't we will install)
    • Sample Code: The attached download contains two examples, one for .Net 1.x and one for .Net 2.0.  The .Net 1.x sample shows coding for using the .NET ODBC driver, which requires some additional coding due to the provider not being originally included in the .Net 1.x framework.  The following Microsoft KB article explains this, and our .Net 1.x sample code includes the additional code to work properly: http://support.microsoft.com/kb/318917
    • There may be some problems using this object within a Visual Studio development environment, as described in the following article: http://www.developerfusion.co.uk/show/3580/1/
  • .Net 2.0:
    • To connect to a MySQL database with ASP.net 2.0 using the native providers within ASP.net 2.0, you cannot use the OleDbConnection provider/class, or the SQLConnection provider/class (which is for MSSQL databases only), instead you need to use the .Net ODBC provider.  Unlike ASP.net 1.x, Microsoft did decide to include this in the .NET 2.0 Framework, and as such, it works by normal usage of that namespace. 
      • Sample Code:  The attached download contains two examples, one for .Net 1.x and one for .Net 2.0. 
    • Additionally, you can use the MySQL.com MySql.Data.MySqlClient, by placing the necessary mysql.data.dll in your application's bin folder.  See http://dev.mysql.com/doc/refman/5.1/en/connector-net-ref.html for more details and support for using this provider.

Sample Code:  In any sample code, please be sure to replace these values with the information for your database:

  • YOUR-DB-NAME
  • YOUR-DB-USER
  • YOUR-PASSWORD
  • YOUR-TABLE

 

 

 
Downloads Associated With This Article
aspnetmysql-sample.zip : ASP.net MySQL db sample code