建立项目后,在sunsonic安装文件中引入Microsoft.Practices.EnterpriseLibrary.Common.dll, Microsoft.Practices.EnterpriseLibrary.Data.dll,Microsoft.Practices.ObjectBuilder.dll,SubSonic.dll四个dll文件即可,注意引入到相应项目文件中。
<configSections> <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" /> </configSections> <connectionStrings configSource="connectionStrings.config"/> <SubSonicService defaultProvider="test"> <providers> <clear/> <add name="test" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="test" generatedNamespace="test" /> </providers> </SubSonicService> <compilation debug="true"> <buildProviders> <add extension=".abp" type="SubSonic.BuildProvider, SubSonic" /> </buildProviders> <assemblies> <add assembly="*"/> </assemblies> </compilation>
<connectionStrings> <add name="test" connectionString="Data Source=(local); Database=test; Integrated Security=true;"/> <add ...> ...... </connectionStrings>
blogstudent.Insert
(数据库各字段值);
单个数据删除: blogstudent.Delete("id",2);
批量数据删除:
Query q = blogstudent.Query(); q.WHERE("name=xuxiuyun"); q.QueryType = QueryType.Delete; q.Execute(); //也可: Query q = new Query("blog_students"); q.BuildDeleteCommand(); q.WHERE("id", 9).WHERE("name","xuxiuyun"); q.Execute();
单个数据更新:
blogstudent=new blogstudent("id"); blogstudent.name="xxy"; blogstudent.save()
批量数据更新:
Query q = BlogStudent.Query(); q.WHERE("name=ssssss").AddUpdateSetting("name", "xuxiuyun"); q.QueryType = QueryType.Update; q.Execute();
subsonic的方法中query的功能最全面(http://subsonichelp.com/),
不仅能够实现各种select语句的交互式参数输入,而且能够实现数据的分页功能:
Query q = BlogStudent.Query(); q.WHERE("name=sssss"); q.QueryType = QueryType.Select; q.PageSize = 2; q.PageIndex = 3;//其中pageIndex从1开始.
subsonic 中使用事务
添加引用 using System.Transactions;
using (TransactionScope scope = new TransactionScope()) { try { OrderSellerList osl1 = new OrderSellerList(); osl1.OrderId = 9; osl1.SellerId = 2; osl1.UserId = 1; osl1.Save(); OrderSellerList osl2 = new OrderSellerList(); osl2.OrderId = 200; osl2.SellerId = 2; osl2.UserId = 1; osl2.Save(); scope.Complete(); } catch{} |