在C#程序中使用MYSQL數(shù)據(jù)庫

2010-08-28 10:47:53來源:西部e網(wǎng)作者:

下面是創(chuàng)建一個數(shù)據(jù)庫鏈接

using MySQLDriverCS;

MySQLConnection conn = new MySQLConnection( new MySQLConnectionString("localhost","test","root","").AsString );

conn.Open();


如果你使用的是.net的集成開發(fā)環(huán)境(visual studio.net)那么在代碼中輸入MySQLDriverCS后再輸入一個”.”就可以看到MySQLDriverCS名稱空間中的所有的東西了.


下面是commond


MySQLCommand cmd;
cmd = new MySQLDriverCS.MySQLCommand("DROP TABLE IF EXISTS test.mysqldrivercs_test",conn);
cmd.ExecuteNonQuery();
cmd.Dispose();

cmd = new MySQLDriverCS.MySQLCommand("CREATE TABLE test.mysqldrivercs_test("+
"SettingID tinyint(3) unsigned NOT NULL auto_increment,"+
"SettingValue text, "+
"PRIMARY KEY (SettingID), UNIQUE KEY SettingID(SettingID), KEY SettingID_2 (SettingID))"+
" TYPE=MyISAM DEFAULT CHARSET=gbk COMMENT='MySQL test table'",conn);

cmd.ExecuteNonQuery();
cmd.Dispose();


下面是insert:


string Value = "Value";
int SettingID = 1;

new MySQLInsertCommand(conn,
new object[,] {{"SettingID",SettingID},{"SettingValue",Value}},
"mysqldrivercs_test"
);


下面是update


Value = "Value2";
new MySQLUpdateCommand(conn,
new object[,] {{"SettingValue",Value}},
"mysqldrivercs_test",
new object[,] {{"SettingID","=",SettingID}},
null
);


下面是select

DataTable dt = new MySQLSelectCommand(conn,
new string[] {"SettingID","SettingValue"},
new string[] {"mysqldrivercs_test"},
new object[,] {{"SettingID","=",SettingID}},
null,
null
).Table;
string storedValue = dt.Rows[0]["SettingValue"].ToString();


下面是delete

new MySQLDeleteCommand(conn,"mysqldrivercs_test",new object[,] {{"SettingID","=",SettingID}},null);


關(guān)閉鏈接


conn.Close();


怎么樣,很方便吧!
關(guān)鍵詞:MYSQLC#

贊助商鏈接: