DBScript:轻量级ORM

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

配置文件:

代码
<? 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 >

 

测试代码:

 

代码
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/

你可能感兴趣的:(script)