add assembly="MySql.Data, Version=5.1.5.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
protected void MySqlCon()
{
//數(shù)據(jù)庫連接字符串跟連接SQL SERVER沒有區(qū)別
string constr = "server=localhost;User Id=root;password=root;Database=test";
//下面使用MySql Connector/net提供的專用對象
MySqlConnection mycon = new MySqlConnection(constr);
mycon.Open();
MySqlCommand mycmd = new MySqlCommand("select * from users", mycon);
MySqlDataReader myreader = mycmd.ExecuteReader();
while (myreader.Read())
{
if (myreader.HasRows)
{
Response.Write(myreader.GetString("email") + "br/>");
}
}
myreader.Close();
mycon.Close();
}
public void Connect_Odbc()
{
//需要事先創(chuàng)建MySQL ODBC DSN.
string odbcString = "DSN=MySQL;";
//string odbcString = "DRIVER={MySQL ODBC 3.51 Driver};" +
// "SERVER=localhost;" +
// "Port=3306;" + //連接本地數(shù)據(jù)庫時可省略此項設(shè)置
// "DATABASE=test;" +
// "UID=root;" +
// "PASSWORD=root;" +
// "OPTION=3";
OdbcConnection odbcConn = new OdbcConnection(odbcString);
odbcConn.Open();
OdbcCommand odbcCmd = new OdbcCommand("SELECT * FROM users", odbcConn);
OdbcDataReader myreader = odbcCmd.ExecuteReader();
while (myreader.Read())
{
if (myreader.HasRows)
{
Response.Write(myreader.GetString(0) + "br/>");
}
}
myreader.Close();
odbcConn.Close();
}