MySqlSugar常用写法
using (var db = SugarDao.GetInstance()) { //转成list List<Student> list1 = db.SqlQuery<Student>("select * from Student"); //转成list带参 List<Student> list2 = db.SqlQuery<Student>("select * from Student where id=@id", new { id = 1 }); //转成dynamic dynamic list3 = db.SqlQueryDynamic("select * from student"); //转成json string list4 = db.SqlQueryJson("select * from student"); //返回int var list5 = db.SqlQuery<int>("select id from Student limit 0,1").SingleOrDefault(); //反回键值 Dictionary<string, string> list6 = db.SqlQuery<KeyValuePair<string, string>>("select id,name from Student").ToDictionary(it => it.Key, it => it.Value); //反回List<string[]> var list7 = db.SqlQuery<string[]>("select id,name from Student limit 0,1").SingleOrDefault(); //存储过程 //var spResult = db.SqlQuery<School>("exec sp_school @p1,@p2", new { p1 = 1, p2 = 2 }); //存储过程加Output //var pars = SqlSugarTool.GetParameters(new { p1 = 1,p2=0 }); //将匿名对象转成SqlParameter //db.IsClearParameters = false;//禁止清除参数 //pars[1].Direction = ParameterDirection.Output; //将p2设为 output //var spResult2 = db.SqlQuery<School>("exec sp_school @p1,@p2 output", pars); //db.IsClearParameters = true;//启用清除参数 //var outPutValue = pars[1].Value;//获取output @p2的值 ////存储过程优化操作 //var pars2 = SqlSugarTool.GetParameters(new { p1 = 1, p2 = 0 }); //将匿名对象转成SqlParameter //db.CommandType = CommandType.StoredProcedure;//指定为存储过程可比上面少写EXEC和参数 //var spResult3 = db.SqlQuery<School>("sp_school", pars2); //db.CommandType = CommandType.Text;//还原回默认 //获取第一行第一列的值 string v1 = db.GetString("select '张三' as name"); int v2 = db.GetInt("select 1 as name"); double v3 = db.GetDouble("select 1 as name"); decimal v4 = db.GetDecimal("select 1 as name"); //.... }