Skip to content
sunkaixuan edited this page May 4, 2019 · 13 revisions

1.use case

//sql
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)
});

//sql  
var dt=db.Ado.GetDataTable("select * from table where id=@id and name=@name",new{id=1,name=2});

//Stored Procedure
var dt2 = db.Ado.UseStoredProcedure().GetDataTable("sp_school",new{name="张三",age=0});//  GetInt SqlQuery<T>  等等都可以用
var nameP= new SugarParameter("@name", "张三");
var ageP= new SugarParameter("@age", null, true);//isOutput=true
var dt2 = db.Ado.UseStoredProcedure().GetDataTable("sp_school",nameP,ageP);

2. Ado Api

name description return type
SqlQuery< T > Query 1 collection,T can be entity or dynamic. List
SqlQuery<T,T2> Query 2 collection Tuple<List, List>
SqlQuerySingle first row T
GetDataTable Query a collection DataTable
GetDataReader Read while using DataReader
GetDataSetAll Query multiple result sets DataSet
ExecuteCommand Number of affected rows int
GetScalar first row first column object
GetString first row first column string
GetInt first row first column int
GetLong first row first column long
GetDouble first row first column Double
GetDecimal first row first column Decimal
GetDateTime first row first column DateTime

3. Tran

3.1 Encapsulation mode

var result = db.Ado.UseTran(() =>
{
    var beginCount = db.Queryable<Student>().Count();
    db.Ado.ExecuteCommand("delete student");
    //throw new Exception("error haha"); 测试代码
});
 
// result.ErrorException
// result.IsSuccess

3.2 Encapsulation mode with result

var result2 = db.Ado.UseTran<List<Student>>(() =>
{
    return db.Queryable<Student>().ToList();
});
// result.ErrorException
// result.IsSuccess
// result.Data
3.3 Manual mode
try
{
 db.Ado.BeginTran();
 ...
 db.Ado.CommitTran();
}
catch (Exception ex)
{
 db.Ado.RollbackTran();
 throw ex;
}

4.Other

db.Ado.CommandTimeOut=30;//Setting timeout time