日期:2009-03-19  浏览次数:20511 次

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


怎么样,很方便吧!