MySoft.Data入门篇:编写业务逻辑

本文介绍如何使用MySoft.Data库进行数据库业务逻辑操作,包括增删改查等基本功能。通过实例演示如何实例化DbSession对象,并展示了不同类型的数据库驱动配置。此外,还详细解释了如何使用Save、Delete、Insert和Update等方法来处理数据。

      接下来要讲讲用MySoft.Data实现数据库业务逻辑,你会发现,用MySoft.Data操作数据库非常简单。在GuestBook这个示例中,我们会实现简单的数据库操作,如增、删、改、查询等。在之前,首先认识一下DbSession。这是   MySoft.Data的数据库操作会话对象,是所有数据库操作的入口。就是说在进行数据库操作时,首先要实例化一个DbSession对象,实例化的方式如下:

DbSession _DbSession
{
     get { return new DbSession("GuestBook"); } 
}

      其中"GuestBook"是你在Web.Config的connectionStrings配置节中配置的数据库连接字符串name:

<connectionStrings> <add name="GuestBook" connectionString="Server=.;Database=GuestBook;Uid=sa;Pwd=benking" providerName="MySoft.Data.SqlServer9.SqlServer9Provider"/>   </connectionStrings>

      providerName 根据自己的数据库类型,配置不同的数据库驱动,MySoft.Data中集成了如下数据库驱动:
             MySoft.Data.SqlServer.SqlServerProvider
             MySoft.Data.SqlServer9.SqlServer9Provider
             MySoft.Data.MsAccess.MsAccessProvider
             MySoft.Data.Oracle.OracleProvider

      如果你需要其他数据库驱动,如MySql、SQLite、FireBird,则需要引用MySoft.Data开发包提供的这三种数据库的程序集:
             MySoft.Data.SQLite.dll(MySoft.Data.SQLite.SQLiteProvider)
             MySoft.Data.MySql.dll(MySoft.Data.MySql.MySqlProvider)
             MySoft.Data.FireBird.dll(MySoft.Data.FireBird.FireBirdProvider)

      增加与更新数据

public void Add(Message pMessage)
{
       _DbSession.Save(pMessage)
}

      将实体保存到数据库,MySoft.Data提供了Save这个泛型方法,可直接将数据插入到数据库。另外,如果你想更新某个实体(注意,不是更新某几个字段),也可以用这个泛型方法。但这里要分几种情况:
      1、首先从数据库取出实体,然后对实体的属性赋值,再更新,调用Save方法就可以成功的更新赋值属性对应的字段,如果实体上没有任何属性赋值,则MySoft.Data认为这是一个异常。
      2、如果你的实体是新实例化的,则在调用Save之前,首先调用实体的Attach属性,这样才能更新成功,否则当插入数据处理。

public void Update(Message pMessage)
{
      pMessage.Attach();
      _DbSession.Save(pMessage);
}

      回头看看实体内部,每个属性的setter都有一个特别的方法:
      this.OnPropertyValueChange(_.GuestName, _GuestName, value);
      这是一个很好的处理,表示只有当给实体属性赋值过的字段,才会更新到数据库。但如果你调用过Attach,则无论是否有更改,所有字段都会被更新。对于增加与修改数据,你还可以使用Insert 和 Update 方法,这些方法可以方便的插入与更新指定字段。

      删除数据

      删除数据比较简单,调用DbSession的Delete即可:

public void Delete(Message pMessage)
{
     _DbSession.Delete(pMessage);
}

      另外,Delete也可以支持传入一个WhereClip子句,WhereClip的语法比较简单,比如我要删除GuestName为benking且留言时间小于2009-10-1的数据:

_DbSession.Delete( Message._.GuestName == "benking" && Message._.AddTime < DateTime.Parse("2009-10-1") );

      注意,子句中“_”是实体里的内部类,这个类专门用于SQL语句的构建。

      查询数据

      查询数据的方法就有很多了,本例中我们只用到了返回IList列表以及查询单个实体的方法:

public Message Get(Int32 pID)
{
      return _DbSession.Get(Message._.ID == pID);
}

public IList GetList()
{
      return _DbSession
            .From()
            .OrderBy(Message._.AddTime.Desc)
            .ToList();
}

public IList GetList(Int32 pPageIndex, Int32 pPageSize)
{
       return _DbSession
            .From()
            .OrderBy(Message._.AddTime.Desc)
            .GetPage(pPageSize)
            .ToList(pPageIndex);
}

public IList GetList(Int32 pTop)
{
      return _DbSession
            .From()
            .OrderBy(Message._.AddTime.Desc)
            .SelectTop(pTop)
            .ToList();
}

      可以看看这一组GetList方法,基于方法模式的MySoft.Data在数据库操作时的语法结构是非常清晰的,而且通俗易懂。采用快速反射,执行效率绝对没有问题。
      由于篇幅原因,具体细节我这里就不再叙述了,有兴趣的朋友可以到官方网站下载一个MySoft.Data试试

      MySoft.Data官方论坛

      嘿,不小心把篇幅弄得太长,今天到此打住。

      原文:http://www.dsjian.com/10002/0027


 

转载于:https://www.cnblogs.com/Benking/archive/2009/10/05/1580035.html

这是正常的?2025-09-10 10:25:57,266 INFO o.j.r.JARSourceHTTP: Found cached repo 2025-09-10 10:25:57,394 INFO o.j.r.PluginManager: Plugins Status: [jpgc-graphs-basic=2.0, jpgc-functions=2.2, jpgc-casutg=3.1.1, jpgc-dummy=0.4, jpgc-ffw=2.0, jpgc-fifo=0.2, jpgc-perfmon=2.1, jpgc-plugins-manager=1.10, jpgc-tst=2.6, jpgc-udp=0.4, jmeter-core=5.6.3, jmeter-ftp=5.6.3, jmeter-http=5.6.3, jmeter-jdbc=5.6.3, jmeter-jms=5.6.3, jmeter-junit=5.6.3, jmeter-java=5.6.3, jmeter-ldap=5.6.3, jmeter-mail=5.6.3, jmeter-mongodb=5.6.3, jmeter-native=5.6.3, jmeter-tcp=5.6.3, jmeter-components=5.6.3, jpgc-standard=2.0] 2025-09-10 10:25:57,394 INFO o.j.r.PluginManagerMenuItem: Plugins Manager has upgrades: [jpgc-plugins-manager] 2025-09-10 10:28:36,500 INFO o.a.j.s.FileServer: Default base='C:\Users\10433' 2025-09-10 10:28:36,501 INFO o.a.j.s.FileServer: Set new base='C:\Users\10433' 2025-09-10 10:28:36,567 INFO o.a.j.s.SaveService: Testplan (JMX) version: 2.2. Testlog (JTL) version: 2.2 2025-09-10 10:28:36,571 INFO o.a.j.s.SaveService: Using SaveService properties file encoding UTF-8 2025-09-10 10:28:36,572 INFO o.a.j.s.SaveService: Using SaveService properties version 5.0 2025-09-10 10:28:38,272 INFO o.a.j.e.StandardJMeterEngine: Running the test! 2025-09-10 10:28:38,273 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-09-10 10:28:38,273 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-09-10 10:28:38,273 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must contain the string: '.functions.' 2025-09-10 10:28:38,273 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must not contain the string: '.gui.' 2025-09-10 10:28:38,413 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-manager-1.11.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,414 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-dummy-0.4.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,415 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-ffw-2.0.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,415 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-graphs-basic-2.0.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,416 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-casutg-3.1.1.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,416 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-manager-1.10.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,417 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-functions-2.2.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,418 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-perfmon-2.1.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,418 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-fifo-0.2.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,421 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-tst-2.6.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,422 INFO o.a.j.r.ClassFinder: Will scan jar C:\mySoft\program\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-udp-0.4.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-09-10 10:28:38,425 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*) 2025-09-10 10:28:38,479 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 线程组 2025-09-10 10:28:38,479 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group 线程组. 2025-09-10 10:28:38,479 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error 2025-09-10 10:28:38,480 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false 2025-09-10 10:28:38,482 INFO o.a.j.t.ThreadGroup: Started thread group number 1 2025-09-10 10:28:38,482 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started 2025-09-10 10:28:38,484 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1 2025-09-10 10:28:38,487 INFO o.a.j.s.SampleResult: Note: Sample TimeStamps are START times 2025-09-10 10:28:38,487 INFO o.a.j.s.SampleResult: sampleresult.default.encoding is set to UTF-8 2025-09-10 10:28:38,487 INFO o.a.j.s.SampleResult: sampleresult.useNanoTime=true 2025-09-10 10:28:38,487 INFO o.a.j.s.SampleResult: sampleresult.nanoThreadSleep=5000 2025-09-10 10:28:39,506 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1 2025-09-10 10:28:39,506 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1 2025-09-10 10:28:39,507 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2025-09-10 10:28:39,507 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*) 2025-09-10 10:29:26,611 INFO o.a.j.e.StandardJMeterEngine: Running the test! 2025-09-10 10:29:26,611 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-09-10 10:29:26,612 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*) 2025-09-10 10:29:26,686 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 线程组 2025-09-10 10:29:26,686 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group 线程组. 2025-09-10 10:29:26,686 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error 2025-09-10 10:29:26,686 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false 2025-09-10 10:29:26,686 INFO o.a.j.t.ThreadGroup: Started thread group number 1 2025-09-10 10:29:26,686 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started 2025-09-10 10:29:26,687 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1 2025-09-10 10:29:27,689 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1 2025-09-10 10:29:27,690 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1 2025-09-10 10:29:27,690 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2025-09-10 10:29:27,690 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值