asp.net连接MySQL数据库
------------------------------------------------------------------------------------------------------------------------------------
asp.net连接MY SQL数据库
-----------------------------------------------------------------------------------------------------------------------------------
连接:
string connStr = "server=localhost;user id=root; password=; database=aa; pooling=false";//
连接字符串
MySqlConnection conn = new MySqlConnection( connStr );//构造数据库连接
try
{
conn.Open();//打开连接
MySqlCommand cmd = new MySqlCommand("select * from list",conn);//构造查询命令
this.DataGrid1.DataSource=cmd.ExecuteReader();//执行查询,返回一个DataReader,设置DataGrid1的数据源为该DataReader
this.DataGrid1.DataBind();//DataGrid1数据绑定
conn.Close();//关闭连接
}
catch(MySqlException ex) //捕获异常
{
Response.Write(ex.Message);//向页面写异常
}