VS 2008 链接 MySQL数据库

1、在mysql官方网站上下载最新的.net链接文件,我这里安装的是mysql-connector-net-6.3.6.msi

2、在web.config中配置链接字符串,示例如下

…… <connectionStrings> <add name="RSSConnectionString" connectionString="Data Source=./SQLEXPRESS;AttachDbFilename="D:/My Documents/RSS.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" /> <add name="MySQLConnectionString" connectionString="Server=localhost;Database=javacourse;User=root;" providerName="MySql.Data.MySqlClient"/> </connectionStrings> ……

3、编写相应的数据库访问代码,更多的内容可以参考安装的mysql-connector手册,和mysql官方网站中的文档,例如在mysql中应用角色,权限管理

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MySql.Data.MySqlClient; namespace DY { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //string connBuilder =@"server=localhost;user=root;database=javacourse;port=3306;"; string connBuilder = System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ToString(); MySqlConnection conn = new MySqlConnection(connBuilder); try { Response.Write("Connecting to MySQL..."); conn.Open(); // Perform databse operations } catch (Exception ex) { Response.Write(ex.ToString()); } conn.Close(); Response.Write("Done."); } } } 

你可能感兴趣的:(VS 2008 链接 MySQL数据库)