Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, August 30, 2006


To connect to a database, a .NET language requires what is called a data provider (kind of database drivers). A data provider is usually packaged in a .NET assembly. A .NET assembly file has a .DLL extension (even on Linux). 

There are two data providers available for MySQL: ByteFX.Data.MySQLClient and Mono.Data.MySQL. Mono’s installation installs the assemblies for both the data providers named ByteFX.Data.dll and Mono. Data.MySql.dll, respectively. However, according to Mono’s website (, Mono.Data. MySQL has been deprecated.

Hence, in this article we use the ByteFX.Data.MySQLClient data provider to connect to MySQL. 

We now explain the steps and syntax to connect and query the database. We assume that you are familiar with the basics of ASP.NET and accessing MS SQL Server using ASP .NET. While going through the following code snippets, notice that the syntax, classes and methods/functions used for querying MySQL are very similar to those used for MS SQL Server. Just the word SQL in the class or method name is replaced with MySQL. For example, SQLConnection becomes MySQLConnection and SQLDataAdpater becomes MySQLDataAdpater. The steps are as follows: 

  • Link the ASP.NET page to the data provider’s assembly using the following statement.

<%@ assembly name="ByteFX.Data" %>

  • Alternatively, you can copy the file named ByteFX.Data.dll found in the directory /usr/lib to /var/www/html/mono/bin. In this case, you don’t need the above statement. 

Import the required namespaces as follows:

<%@ import namespace="ByteFX.Data.MySQLClient" %>

  • Specify the connection parameters as:

MySQLConnection conn = new MySQLConnection("Server=localhost;Database=pcquest;User ID=root;Password=foo;");

In the above statement, replace localhost with the name or IP address of the machine running the MySQL database. Replace pcquest with the name of the database you wish to connect to. Substitute root and foo with the username and password required to connect to the database. 

Issuing Insert, Update and Delete Queries
Now we see how to issue SQL queries, which don’t return data from the database like Insert, Update and Delete queries.

  • Construct the SQL query using the following statement: 


, , , , ,

Post a Comment