组件V1.7版本增加对MySQL的支持(测试了MySQL 5.1版本)。
下面来演示如何使用。
首先添加Connectionstring
<add name="MySQLConnectionString" connectionString="server=192.168.1.175;user id=root; password=huxj; Port=3306;database=test;" providerName="Hxj.Data.MySql" />
public static DbSession MySqlTest = new DbSession("MySQLConnectionString");
在test数据库中新建了一张表mtable,字段只有两个:mid int 自增长,mname varchar(50)
然后使用实体生成工具生成实体。
测试添加数据,代码如下:
Mtable m = new Mtable(); m.Mname = Hxj.Common.StringHelper.GetRandomString(); int returnValue = Db.MySqlTest.Insert<Mtable>(m);
生成的sql如下:
Text:
INSERT INTO `mtable` (`mname`) VALUES (?swbeetoamhazjwa);select last_insert_id();
Parameters:
?swbeetoamhazjwa[String] = wzfJPJIDvKPP15HKRyxO4w==
再测试批量添加:
using (DbBatch batch = Db.MySqlTest.BeginBatchConnection(5)) { for (int i = 0; i < 10; i++) { Mtable m = new Mtable(); m.Mname = Hxj.Common.StringHelper.GetRandomString(); batch.Insert<Mtable>(m); } }
生成的sql如下:
Text:
INSERT INTO `mtable` (`mname`) VALUES (?tksvpwwdiguovrlq);INSERT INTO `mtable` (`mname`) VALUES (?ttnbgzirzhvlssuv);INSERT INTO `mtable` (`mname`) VALUES (?eotxpjjzywvzzcro);INSERT INTO `mtable` (`mname`) VALUES (?xemplfkdzouwbwzx);INSERT INTO `mtable` (`mname`) VALUES (?ulyiapnhhwkufzxn);
Parameters:
?tksvpwwdiguovrlq[String] = oYCY91wzpUnDA5TOiPtL9g==
?ttnbgzirzhvlssuv[String] = TEtLPO3QvuUEGeHTuoufhw==
?eotxpjjzywvzzcro[String] = k0Gs4Ndf4Kjp/1m973CMwA==
?xemplfkdzouwbwzx[String] = MIH1LPkpa2HwPnIJmNUmnA==
?ulyiapnhhwkufzxn[String] = NIni89feCPwRLspG1BFevg==
Text:
INSERT INTO `mtable` (`mname`) VALUES (?rkqfafnpxtfyqaka);INSERT INTO `mtable` (`mname`) VALUES (?ymfmodryykkiozcc);INSERT INTO `mtable` (`mname`) VALUES (?ztwdwxcwnkrdfzvd);INSERT INTO `mtable` (`mname`) VALUES (?pugtkuoehuqavxch);INSERT INTO `mtable` (`mname`) VALUES (?igthuzfzqopddexr);
Parameters:
?rkqfafnpxtfyqaka[String] = u/rxTfVKLkvymn5+U3gARA==
?ymfmodryykkiozcc[String] = ypsRx4kDZPjH/MRrwoHegA==
?ztwdwxcwnkrdfzvd[String] = DnzF82DWq5313Vg7LWPKxA==
?pugtkuoehuqavxch[String] = qNQ058RJ4fZSDQocALGVaQ==
?igthuzfzqopddexr[String] = 9fv5ZpttbcicvT5M573jGA==
5条sql语句组合执行一次。
然后进行查询:
Db.MySqlTest.From<Mtable>().Page(10, 2).ToList();
Text:
SELECT * FROM `mtable` ORDER BY `mtable`.`mid` ASC limit 10,10
其他的示例就不再一一上代码了,操作都是与以前一样的。