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();
}
}
}