一提起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();
怎么样,很方便吧!