Can Anybody tell me how to connect ASP to MySQL database.. I have already install MySQL in my localhost, A开发者_开发知识库dd a connection through Data Source (ODBC) in ctrl panel and test connection succeed. Now I want to show the query on my default.aspx page so that I can do the insert / update / delete my table data in MySQL. Can anyone help me?
The recommended way of connecting to MySQL in .NET is using ADO.NET connector. You could download it from here. It represents a .NET assembly that you need to reference in your application. And here's a sample code:
using (var cn = new MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"))
using (var cmd = cn.CreateCommand())
{
cn.Open();
cmd.CommandText = "select * from sometable";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// read values
}
}
}
精彩评论