SqlSugar文檔


sqlsugar:

http://www.donet5.com/Doc/1/1198

 

 

介紹

下面的方法支持復雜的Sql  、 返回多個結果集 、存儲過程等  、可以理解為了一個高級DbHelper

 

1、用法介紹

//調用Sql
db.Ado.具體方法
 
//調用存儲過程
db.Ado.UseStoredProcedure().具體方法

 

2、調用Sql

//參數1
var  dt=db.Ado.GetDataTable( "select * from table where id=@id and name=@name" , new  List<SugarParameter>(){
   new  SugarParameter( "@id" ,1),
   new  SugarParameter( "@name" ,2)
});
 
//參數2
var  dt=db.Ado.GetDataTable( "select * from table where id=@id and name=@name" , new {id=1,name=2});
 
 
//原生SQL用實體
var   t=db.Ado.SqlQuery<table>(sql); //比db.SqlQueryable兼容性更強,支持復雜SQL存儲過程,缺點沒有自帶的分頁操作

 

3、 調用存儲過程

//簡單用法
var  dt = db.Ado.UseStoredProcedure().GetDataTable( "sp_school" , new {name= "張三" ,age=0}); 
 
 
 
//帶有output的存儲過程
var  nameP=  new  SugarParameter( "@name" "張三" );
var  ageP=  new  SugarParameter( "@age" null true ); //設置為output
var  dt = db.Ado.UseStoredProcedure().GetDataTable( "sp_school" ,nameP,ageP);
//ageP.Value可以拿到output值
//Oracle 游標參數用法  
 
 
//如果是ReturnValue
var  nameP= new  SugarParameter( "@name" "張三" typeof ( string ),ParameterDirection.ReturnValue);
 
 
//我們還可以用 GetParameters 來簡化參數操作 
  SugarParameter [] pars =db.Ado.GetParameters( new {p=1,p2=p}); 
  pars[1].Direction=ParameterDirection.Output;

 

4、in參數用法

  var  dt = db.Ado.SqlQuery<Order>(
                       "select * from [order] where  id in(@ids)" ,
                        new  { ids =  new  int [] { 1,2,3} });
  //select * from [order] where  id in('1','2','3')

 

5、db.Ado下面的所有方法

用法和上面一樣只是方法名換一下

方法名 描述 返回值
SqlQuery< T > 查詢所有返回實體集合 List
SqlQuery<T,T2> 可以返回2個結果集 Tuple<List, List>
SqlQuerySingle 查詢第一條記錄 T
GetDataTable 查詢所有 DataTable
GetDataReader 讀取DR需要手動釋放DR DataReader
GetDataSetAll 獲取多個結果集 DataSet
ExecuteCommand 返回受影響行數,一般用於增刪改 int
GetScalar 獲取首行首列 object
GetString 獲取首行首列 string
GetInt 獲取首行首列 int
GetLong 獲取首行首列 long
GetDouble 獲取首行首列 Double
GetDecimal 獲取首行首列 Decimal
GetDateTime 獲取首行首列 DateTime

 

6、SqlServer帶Go的腳本處理

db.Ado.ExecuteCommandWithGo(sql)  //go語句是獨立一行就支持

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM