- You need to download MySQL Connector/Net .
- 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.
- 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;
}
- Then create an instance from MySql.Data.MySqlClient.MySqlConnection as shown below.
MySql.Data.MySqlClient.MySqlConnection mycon
= new MySqlConnection( GetConnectionString());
if(mycon .State != ConnectionState.Open)So simple as you see, It is always better to do this in data access layer also called DAL.
try
{
mycon .Open();
}
catch (MySqlException ex)
{
throw (ex);
}
Connect Mysql in C# and ASP.NET.
10 comments:
Thanks
thanks for wonderful blog
thanks for wonderful blog
You are so excellent and your this post is so mind blowing!
because i was search for connection string which connects mysql databse with asp.net!
All good, except there is no MySqlException in package MySql.Data.MySqlClient.
Thanks dude... :)
Thanks dude
Thanks a Lot
Very good job....
I'm using Microsoft Visual Web Developer 2008 Express Edition & I keep getting an error at "using MySql.Data.MySqlClient;" - do I need to add something to the dependencies ?
Post a Comment