November 8, 2011 05:18 by
Scott
In this tutorial, I will show you how to connect to MySQL Server with .NET in C# or ASP.NET. What requirements do you need?
1. Please download MySQL Connector/Net.
2. After you add a reference to your project, it is probably in C:\Program Files\MySQL\MySQL Connector Net 5.0.7\Binaries\.NET 2.0 folder, add the MySql.Data.dll file as a reference.
3. Make your connection string, the following code will shows a standard MySQL connection string.
using MySql.Data.MySqlClient;
public static string GetConnectionString()
{
string connStr =
String.Format("server={0};user id={1}; password={2};
database=yourdb; pooling=false", "yourserver",
"youruser", "yourpass");
return connStr;
}
4. Then create an instance from MySql.Data.MySqlClient.MySqlConnection as shown below.
MySql.Data.MySqlClient.MySqlConnection mycon
= new MySqlConnection( GetConnectionString());
5. Then try to open the MySQL connection.
if(mycon .State != ConnectionState.Open)
try
{
mycon .Open();
}
catch (MySqlException ex)
{
throw (ex);
}
Simple right?? J