一提起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();