DBScript:轻量级ORM

    DBScript是一个轻量级的ORM,支持数据库的以下操作:1.基本的增删改查 2. 数据库事务 3.数据库的读写分离。暂时支持的数据库有:MsSQL2005,MySQL,SQLite.

配置文件:

ExpandedBlockStart.gif 代码
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
  
< configSections >
    
< section  name ="DBScript"  type ="DBScript.DBScriptSectionHandler,DBScript"   />
  
</ configSections >
  
  
< DBScript >
    
< setting  IsDebug ="false"  DebugFilePath ="/Log/"   />
    
< add  name ="Test_MsSQL"  connectionString ="Database=DBTest;Server=WORKGROU-AXFN59\XUZHIBIN;Integrated Security=false;user id=sa;Password=sa;"  dbType ="MsSQL"   />
    
< add  name ="Test_MsSQL2k"  connectionString ="Database=DBTest;Server=WORKGROU-AXFN59\XUZHIBIN;Integrated Security=false;user id=sa;Password=sa;"  dbType ="MsSQL2k"   />
    
< add  name ="Test_MySQL"  connectionString ="Data Source=127.0.0.1;User ID=root;Password=123456;DataBase=DBTest;Charset=utf8;"  dbType ="MySQL"   />
    
< add  name ="Test_SQLite"  connectionString ="Data Source=H:\temp\DBTest.s3db"  dbType ="SQLite"   />
  
</ DBScript >
</ configuration >

 

测试代码:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  NUnit.Framework;
using  DBScript;
using  UnitTest.Model;

namespace  UnitTest.DBScriptTest
{
    
///   <summary>
    
///  基本操作测试
    
///   </summary>
    [TestFixture]
    
public   class  MsSqlTest
    {
        
///   <summary>
        
///  Add
        
///   </summary>
        [Test]
        
public   void  AddTest()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            Random rd
= new  Random();
            UserTest us 
=   new  UserTest();
            us.Name 
=   " TUser " + rd.Next( 1 999 );
            us.Password 
=   " 123456 " ;
            us.Email 
=   string .Format( " {0}@test.com " ,us.Name);
            us.Information 
=   " 测试用户 " ;
            us.CreateTime 
=  DateTime.Now;
            us.UpdateTime 
=  DateTime.Now;
            us.IsSuper 
=   true ;
            us.State 
=   1 ;

            Assert.IsTrue(dal.Add
< UserTest > (us) > 0 );
        }
        
///   <summary>
        
///  Delete
        
///   </summary>
        [Test]
        
public   void  DeleteTest()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            dal.Delete
< UserTest > ( 1 );
        }
        
///   <summary>
        
///  Update
        
///   </summary>
        [Test]
        
public   void  UpdateTest()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            UserTest us 
=  dal.GetModel < UserTest > ( 10 );
            us.Information 
=   " 已修改 " ;
            dal.Update
< UserTest > (us);
        }
        
///   <summary>
        
///  GetList
        
///   </summary>
        [Test]
        
public   void  GetListTest1()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            List
< UserTest >  users  =  dal.Page( 1 5 ).GetList < UserTest > ();
            
foreach (UserTest us  in  users)
            {
                Console.WriteLine(us.Name);
            }
        }
        
///   <summary>
        
///  GetList
        
///   </summary>
        [Test]
        
public   void  GetListTest2()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            List
< UserTest >  users  =  dal.Page( 2 5 ).OrderBy( " Name desc " ).GetList < UserTest > ();
            
foreach  (UserTest us  in  users)
            {
                Console.WriteLine(us.Name);
            }
        }
        
///   <summary>
        
///  GetList
        
///   </summary>
        [Test]
        
public   void  GetListTest3()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            List
< UserTest >  users  =  dal.Where( " name like @name " ).SetString( " name " " %6% " ).GetList < UserTest > ();
            
foreach  (UserTest us  in  users)
            {
                Console.WriteLine(us.Name);
            }
        }
        
///   <summary>
        
///  GetList
        
///   </summary>
        [Test]
        
public   void  GetListTest4()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            List
< UserTest >  users  =  dal.Select( " Name " ).GroupBy( " Name " ).Having( " count(*)>3 " ).OrderBy( " Name desc " ).Page( 1 5 ).GetList < UserTest > ();
            
foreach  (UserTest us  in  users)
            {
                Console.WriteLine(us.Name);
            }
        }
        
///   <summary>
        
///  GetListAndCount
        
///   </summary>
        [Test]
        
public   void  GetListAndCountTest()
        {
            
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            ArrayList attrs 
=  dal.Where( " name like @name " ).SetString( " name " " %1% " ).Page( 1 5 ).GetListAndCount < UserTest > ();
            List
< UserTest >  users  =  attrs[ 0 as  List < UserTest > ;
            
int  count  =  ( int )attrs[ 1 ];
            
foreach  (UserTest us  in  users)
            {
                Console.WriteLine(us.Name);
            }
            Console.WriteLine(count);

        }
        
///   <summary>
        
///  GetListObjects
        
///   </summary>
        [Test]
        
public   void  GetListObjectsTest()
        {

            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            List
< object [] >  listObjs  =  dal.From( " UserTest " , " UserId " ).Select( " Name,count(*) as ct " ).GroupBy( " Name " ).Having( " count(*)>1 " ).OrderBy( " Name desc " ).Page( 1 5 ).GetListObjects();
            
foreach  ( object [] objs  in  listObjs)
            {
                Console.WriteLine(
string .Format( " {0}:{1} " ,objs[ 0 ],objs[ 1 ]));
            }

        }
        
///   <summary>
        
///  GetCount
        
///   </summary>
        [Test]
        
public   void  GetCountTest()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            Console.WriteLine(dal.From
< UserTest > ().GetCount());
        }
        
///   <summary>
        
///  Transaction(Yes)
        
///   </summary>
        [Test]
        
public   void  TransactionTest1()
        {
            IDBBase dal 
=  DBFactory.GetDbProvider( " Test_MsSQL " );
            dal.BeginTransaction();
            UserTest us 
=  dal.GetModel < UserTest > ( 15 );
            us.Information 
=   " Transaction " ;
            dal.Update
< UserTest > (us);
            dal.Delete
< UserTest > ( 2 );
            dal.Commit();
        }
    }
}

 


 

源码及测试代码下载

 

--------------------------------------

 http://www.qwolf.com/

posted on 2010-02-22 20:52 xuzhibin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/xuzhibin/archive/2010/02/22/1671503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值