Home > MySQL > Connecting to MySQL using C#.

Connecting to MySQL using C#.

To open an ODBC connection from Windows 2003 to a MySQL database, use the following piece of C# code:-

string connString = “DRIVER={MySQL};SERVER=servername;DATABASE=dbname;UID=mysqlusername;PWD=mysqlpassword”;
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(connString);

You will need to set servername, dbname, mysqlusername and mysqlpassword to suit your database as detailed for MySQL

Please remember to call conn.Close(); to close the connection afterwards