dos.ORM配置和使用

處理oralce,sqlserver,access及其餘經常使用數據庫,下載和學習地址

一、web.config配置數據庫鏈接字符串,以及數據庫類型:
<connectionStrings>
<!--MySql-->
<add name="DosConn" providerName="Dos.ORM.MySql" connectionString="Data Source=127.0.0.1;Database=DBName;User Id=root;Password=root;" /> //未檢驗
<!--SqlServer-->
<add name="DosConn" providerName="Dos.ORM.SqlServer9" connectionString="server=127.0.0.1;database=DBName;uid=root;pwd=root;" /> //檢驗能夠
<!--SqlServer-->

<add name="DosOracle" providerName="Dos.ORM.Oracle" connectionString="User ID=AQPU;Password=AQPU;Data Source=orcl;Max Pool Size=512"/> //經檢驗能夠
</connectionStrings>

 

組件讀取connectionStrings節點的配置的時候會根據providerName來判斷是哪一種數據庫。
  • Sql server 2000:Dos.ORM.SqlServer
  • Sql server 2005/2008及以上:Dos.ORM.SqlServer9
  • MS Access:Dos.ORM.MsAccess
  • Sqlite:Dos.ORM.Sqlite
  • Oracle:Dos.ORM.Oracle
  • MySql:Dos.ORM.MySql
二、全局聲明
public class DB {     

  public static readonly DbSession Context = new DbSession("DosConn");
}

 

三、開始使用Dos.ORM
var list = DB.Context.From<Dos.Model.TableName>().ToList();

四、使用示例html

全局配置:
public class DB
{
    public static readonly DbSession Context = new DbSession("DosConn");
}
查詢示例(Lambda表達式寫法):
DB.Context.From<Dos.Model.TableName>()
    .Select(d => new { d.ID, d.Price })    //select ID,Price from TableName
    .Where(d => (d.ID == 2 && d.Name != "itdos" 
                    && d.Name.In("com","net","cn") && ) 
            || d.Sex != null)
    // where (id=2 and Name<>'itdos' and Name in('com','net','cn')) or Sex is not null
    .GroupBy(d => new { d.Name, d.Sex })    //group by Name,Sex
    .OrderBy(d => new { d.CreateTime, d.Name })    //order by CreateTime,Name
    .Having(d => d.Name != '')    //having Name<>''
    .Top(5)
    .Page(10, 2)    //分頁返回結果 每頁10條返回第2頁數據
    .ToList();    //返回實體列表
    //.ToFirst();    //返回第一個實體
    //.ToFirstDefault();    //返回第一個實體,若是爲null,則默認實例化一個
    //.ToDataSet();    //返回DataSet
    //.ToDataReader();    //返回IDataReader
    //.ToDataTable();    //返回DataTable
    //.ToScalar();    //返回單個值
插入示例:
//單個實體插入
var newModel = new Dos.Model.TableName(){
    ID = Guid.NewGuid(),
    Name = "ITdos.com"
};
var count1 = DB.Context.Insert<Dos.Model.TableName>(newModel);
//批量插入 var newList = new List<Dos.Model.TableName>(); newList.Add(new Dos.Model.TableName(){ ID = Guid.NewGuid(), Name = "ITdos.com" }); var count2 = DB.Context.Insert<Dos.Model.TableName>(newList);
刪除示例:
var count1 = DB.Context.Delete<Dos.Model.TableName>(d => d.ID == 1);
修改示例:
var uptModel = DB.Context.From<Dos.Model.TableName>().Where(d => d.ID == 1).ToFirst(); uptModel.Attach(); uptModel.Name = "ITdos.net"; var count1 = DB.Context.Update<Dos.Model.TableName>(uptModel);
相關文章
相關標籤/搜索