1.下载mysql连接库
下载链接here
2.添加MySql.Data库。
右击 项目—>添加—>引用—>扩展—>选择MySql.Data(第一个)
3.配置webconfig.xml
在configration结点下添加结点:
<connectionStrings>
<add name="connection" connectionString="Data Source=localhost;Initial Catalog=database_name;Persist Security Info=True;User ID=root;Password=123456" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
4.代码中:(label1用来显示调试信息)
using MySql.Data;
using MySql.Data.MySqlClient;
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"]
.ConnectionString.ToString());
try
{
connection.Open();
this.Label1.Text = "连接数据库成功!";
}
catch(Exception err)
{
Label1.Text = "连接数据库失败!";
Label1.Text += err.Message;
}
finally
{
connection.Close();
}
}