利用MySQLDriverCS实现在.NET环境下访问MySQL数据库

本文介绍如何在.NET环境中使用MySQLDriverCS库连接并查询MySQL数据库。通过具体代码示例展示了连接配置、设置字符集及执行SQL语句的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,安装,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中就OK了。也许你对这行代码

MySQLCommand commn = new MySQLCommand("set names gb2312",conn);

        commn.ExecuteNonQuery();

不太理解。这是因为如果没有这句话从数据库里读出的数据中如果有中文将显示乱码,只要把这个语句“set names gb2312”当作SQL语句执行一遍就可以了。


转载于:https://www.cnblogs.com/V587/archive/2011/04/18/2020299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值