<add assembly="MySql.Data, Version=5.1.5.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
protected void MySqlCon()
{
//数据库连接字符串跟连接SQL SERVER没有区别
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()
{
//需要事先创建MySQL ODBC DSN.
string odbcString = "DSN=MySQL;";
//string odbcString = "DRIVER={MySQL ODBC 3.51 Driver};" +
// "SERVER=localhost;" +
// "Port=3306;" + //连接本地数据库时可省略此项设置
// "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();
}