先下载和安装MySQLDriverCS,地址:
http://sourceforge.net/projects/mysqldrivercs/
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySQLDriverCS;
using System.Data.Common;
namespace Message
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MySQLConnection conn = null;
try
{
string constr = "Data Source=stu;User ID=root;Password=myd";//Data Source后面是数据库名
//conn = new MySQLConnection(new MySQLConnectionString("localhost", "stu", "root", "myd").AsString);//另外一种写法
conn = new MySQLConnection(constr);
conn.Open();
string sql = "select * from tab_stu";
MySQLDataAdapter adt = new MySQLDataAdapter(sql,conn);
DataSet ds = new DataSet();
adt.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alter('"+ex+"');</script>");
}
finally
{
conn.Close();
}
}
}
}