using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySQLDriverCS;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("127.0.0.1", "Dvbbs", "root", "123456").AsString);
conn.Open();
MySQLCommand commn = new MySQLCommand("set names gb2312",conn);
commn.ExecuteNonQuery();
string sql = "select title,topicID, boardId ,dateandtime,hits  from  Dv_topic order by dateandtime desc limit 10";
MySQLDataAdapter mda = new MySQLDataAdapter(sql,conn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
conn.Close();
}
}
就这样就可以实现在.NET环境下对MySQL数据的访问。在这之前还要做一些准备,下载MySQLDriverCS,下载地址http://www.sourceforgecn.net/Projects/m/my/mysqldrivercs/安装,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中就OK了。也许你对这行代码
MySQLCommand commn = new MySQLCommand("set names gb2312",conn);
commn.ExecuteNonQuery();
不太理解。这是因为如果没有这句话从数据库里读出的数据中如果有中文将显示乱码,只要把这个语句“set names gb2312”当作SQL语句执行一遍就可以了。