» MySQL and .NET - Using MySQLDriverCS by sde |
|
(Login to remove green text ads)
As of now (Feb 2004), there are a couple different free MySQL APIs for .NET. I have tested both MySQLDriverCS and ByteFX.Data. MySQLDriverCS ( http://sourceforge.net/projects/mysqldrivercs/ ) performed much faster so I will show you how to use it here.
First download the driver from the link above.
You will need to add a reference to MySQLDriverCS in your Solution Explorer. To do this, Right Click on References in your Solution Explorer and choose Add Reference. Click Browse and locate the MySQLDriverCS dll and Click Select. Then Click OK.
The next step is to include the MySQLDriverCS namespace in the code.
Code:
using MySQLDriverCS;
For the most part, MySQLDriverCS mirrors the MSSQL and ODBC methods of database interaction. Below is an example of using MySQLReader to read through query results.
Code:
try
{
MySQLConnection con;
con = new MySQLConnection( new MySQLConnectionString("localhost",
"database",
"user",
"pass").AsString );
con.Open();
string sql = "SELECT * FROM Table";
MySQLCommand cmd = new MySQLCommand(sql,con);
MySQLDataReader reader = cmd.ExecuteReaderEx();
while(reader.Read())
{
Console.WriteLine( reader[0].ToString() );
}
reader.Close();
con.Close();
}
catch(Exception ee)
{
Console.WriteLine( ee.ToString() );
}
|
|