C# 链接MySql数据库

C# 链接MySql数据库只得注意的几点:

1、C#链接MySql数据库要在网上下载一个mysql-connector-net-6.0.4-noinstall.rar  这里面放的都是一堆dll .将他们全部放在ProjectBin  然后在VS里引入一下就OK啦~  对了mysql.data.cf.dll这个除外(不要引用)

2、进行数据库链接的时候注意了,c#链接MySql是和链接SQl的代码是不一样的。

    c#链接MySql是这样的:

 string MySqlString = "User Id=a;pwd=a;Host=服务器;Port=服务器端口号;Database=数据库;Character Set=utf8";

 

下面试个实例:

 

using MySql.Data.MySqlClient;


namespace Discuz
{
    public partial class _Default : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Bind();
            }
        }

        public void Bind()
        {
          
            string MySqlString = "User Id=dis;pwd=sa;Host=1.2.3.4;Port=6033;Database=dis;Character Set=utf8";
            MySqlConnection conn = new MySqlConnection(MySqlString);
            conn.Open();
            string bb = "SELECT p.author, p.message FROM cdb_threads AS t INNER JOIN cdb_posts AS p ON t.tid = p.tid where t.fid = 34 and digest !=0";
            MySqlDataAdapter sda = new MySqlDataAdapter(bb, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "T");

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

             conn.close();
        }
    }
}
 

posted @ 2014-02-26 20:12  zzg168  阅读(155)  评论(0编辑  收藏  举报