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