Connecting to the MySQL server with ASP.NET

The following is example of an ODBC connect string:

connString = "Driver={MySQL ODBC 5.3 ANSI Driver};server=mysql.cms.gre.ac.uk;user=username;password=YourMySQLPassword;database=mdb_username;option=3";

Replace ‘username’ and ‘password’ with your own username and MySQL password.

The following is an example of C# code user aaa001 would use for connecting to MySQL:

string connString;
connString = "Driver={MySQL ODBC 5.3 ANSI Driver};server=mysql.cms.gre.ac.uk;user=aaa001;password=MySQLPassword;database=mdb_aaa0001;option=3";

OdbcConnection myConnection = new OdbcConnection(connString);
myConnection.Open();

string myQuery = "SELECT username FROM users";
string theUser = null;

OdbcCommand myCommand = new OdbcCommand(myQuery, myConnection);

OdbcDataReader reader = myCommand.ExecuteReader();
    
while (reader.Read())
{
	theUser = reader["UserName"].ToString();
    TextBox1.Text = TextBox1.Text + " " + theUser;
}
myConnection.Close();

For security reasons, you should ensure you use a different password for your SQL Server login than you do for other University systems.

For more information and a reference of all the commands available, go to the MySQL documentation page.

Comments are closed.