Connection to MySQL with .NET Connector

Code example of connecting to MySQL via .NET Connector, and fix for the 'SELECT command denied' error via Use Procedure Bodies=false in the connection string.

The following code example can be used to gain access to a MySQL Database through MySQL's .NET Connector:

You can deploy the MySQL driver to your webhosting plan yourself; it can be downloaded from MySQL's website.

using MySql.Data.MySqlClient;

MySqlConnection con = new MySqlConnection();
string mysql = "Server=[server];Database=[database];User=[brugernavn];pwd=[password]";
con.ConnectionString = mysql;
con.Open();

MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM TestTabel";
cmd.Connection = con;

MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
Response.Write(reader.GetValue(0).ToString());
}

cmd.Dispose();
con.Close();

Error: SELECT command denied to user 'user'@'x' for table 'proc'

The cause is a bug in the MySQL driver. Documented here:
http://bugs.mysql.com/bug.php?id=10640
http://bugs.mysql.com/bug.php?id=16703
http://bugs.mysql.com/bug.php?id=14836

A process attempts to execute a SELECT statement on the proc table. This 'proc' table is a system table that stores procedures for all users on the MySQL server. On shared hosting, access to this table is a problem, as access would allow customers to see each other's procedures.

A possible solution to the problem is to change your connection string so that it contains "Use Procedure Bodies=false".

Example:
"server=localhost; user id=dbuser; password=password; database=dbname; Use Procedure Bodies=false;"

Article from the support category: ASP & ASP.NET