一提起mysql資料庫,好像就是在說java,jsp或者php,其實在所有程式中都可以使用mysql 資料庫,其它的程式使用my sql資料庫的方法是使用my sql 提供的odbc驅動,在.net 中可以使用類似於jdbc似的東東.它的名字是:MySQLDriverCS.dll.把它添加到.net的組件面板裡,至於如何添加,則不是本篇的任務了(^_^)!
下面是建立一個資料庫連結:using MySQLDriverCS;
MySQLConnection conn = new MySQLConnection( new MySQLConnectionString("localhost","test","root","").AsString );
conn.Open();
如果你使用的是.net的整合式開發環境(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 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);
關閉連結:conn.Close();