c# mysql 串連
來源:互聯網
上載者:User
MySQLConnection conn = null;
try
{
//MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("myquest", "root", "amttgroup").AsString);
//執行個體化一個連線物件其中myquest為資料庫名,root為資料庫使用者名稱,amttgroup為資料庫密碼
string connstr = "Data Source=MySQL;Password=root;User ID=root;Location=localhost";
conn = new MySQLConnection(constr);
conn.Open();
string query = "insert into test.dbtable values(10, 'disksidkfsdi', 'asdfaf', 'adsfasdf')";
string tmp = null;
MySQLCommand cmd = new MySQLCommand(query, conn);
for(int i = 0; i < 100000; i++)
{
cmd.ExecuteNonQuery();
}
cmd.Dispose();
conn.Close();
query = "select * from test.dbtable";
MySQLCommand cmd2 = new MySQLCommand(query, conn);
conn.Open();
//設定中文輸出
MySQLCommand firstCmd = new MySQLCommand("set names gb2312", myConn);
firstCmd.ExecuteNonQuery();
MySQLDataReader reader = cmd2.ExecuteReaderEx();
while(reader.Read())
{
tmp = reader[0].ToString();
tmp = reader[1].ToString();
tmp = reader[2].ToString();
tmp = reader[3].ToString();
}
conn.Close();
query = "delete from test.dbtable";
MySQLCommand cmd3 = new MySQLCommand(query, conn);
conn.Open();
cmd3.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}