/// <summary> /// 查询单条记录 /// </summary> /// <param name="ExpWhere">lamda表达式</param> /// <param name="SqlOrderBy">取值排序字段,如:Id Asc,CreateTime Desc</param> /// <param name="UseReadonlySource">是否实用只读数据源,true-是,false-否</param> /// <returns></returns> public static T Get(Expression <Func <T, bool> > ExpWhere, string SqlOrderBy, bool UseReadonlySource = true) { ResolveExpress re = new ResolveExpress(); re.ResolveExpression(re, ExpWhere); string SqlWhere = "1=1" + re.SqlWhere; object[] SqlParams = re.SqlParams.ToArray(); string SqlText = SqlGenerator.SQLBUILDER_Get(DB.ORM_TabInfo, SqlWhere, SqlOrderBy); Data.DBHelper db = new Data.DBHelper(DB.ORM_TabInfo.ORMConnectionMark, UseReadonlySource); Data.Common.DataTable dt = db.ExecTextDataTable(SqlText, SqlParams); if (dt != null && dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; DynamicBuilder <T> DyBuilder = DynamicBuilder <T> .CreateBuilder(dr); T Model = DyBuilder.Build(dr); ModelBase <T> TempModel = Model as ModelBase <T>; TempModel.ModifiedColumns.Clear(); return(Model); } else { return(new T()); } }
/// <summary> /// 查询分页记录 /// </summary> /// <param name="DWhere">动态条件</param> /// <param name="SqlOrderBy">取值排序字段,如:Id Asc,CreateTime Desc</param> /// <param name="RecordCount">数据记录总数</param> /// <param name="PageIndex">页码,默认1</param> /// <param name="PageSize">页大小,默认20</param> /// <param name="UseReadonlySource">是否实用只读数据源,true-是,false-否</param> /// <returns></returns> public static List <T> List(dynamic DWhere, string SqlOrderBy, out long RecordCount, int PageIndex = 1, int PageSize = 20, bool UseReadonlySource = true) { List <object> SqlParams = new List <object>(); string SqlText = SqlGenerator.SQLBUILDER_List(DB.ORM_TabInfo, DWhere, SqlOrderBy, PageIndex, PageSize, out SqlParams); Data.DBHelper db = new Data.DBHelper(DB.ORM_TabInfo.ORMConnectionMark, UseReadonlySource); List <object> TempParams = new List <object>(); if (SqlParams != null) { TempParams.AddRange(SqlParams); } TempParams.Add(PageIndex * PageSize - PageSize + 1); TempParams.Add(PageIndex * PageSize); DataSet ds = db.ExecTextDataSet(SqlText, TempParams.ToArray()); List <T> ReturnObjs = new List <T>(); RecordCount = -1; if (ds != null && ds.Tables.Count == 2) { XORM.CBase.Data.Common.DataTable DataDT = ds.Tables[0]; XORM.CBase.Data.Common.DataTable CountDT = ds.Tables[1]; if (DataDT != null && DataDT.Rows.Count > 0) { foreach (XORM.CBase.Data.Common.DataRow dr in DataDT.Rows) { DynamicBuilder <T> DyBuilder = DynamicBuilder <T> .CreateBuilder(dr); T Model = DyBuilder.Build(dr); ModelBase <T> TempModel = Model as ModelBase <T>; TempModel.ModifiedColumns.Clear(); ReturnObjs.Add(Model); } } if (CountDT != null && CountDT.Rows.Count == 1) { long.TryParse(CountDT.Rows[0][0].ToString(), out RecordCount); } else { RecordCount = 0; } } return(ReturnObjs); }
public Common.DataTable ExecDataTable(DbCommand cmd) { try { cmd.Connection = this.SQLConn; this.Open(); MySqlDataAdapter adp = new MySqlDataAdapter((MySqlCommand)cmd); Common.DataTable dt = new Common.DataTable(); adp.Fill(dt); this.Close(); return(dt); } catch (Exception e) { this.Close(); throw e; } }
public XORM.CBase.Data.Common.DataTable ExecDataTable(DbCommand cmd) { try { cmd.Connection = this.SQLConn; this.Open(); SqlServerDataAdapter adp = new SqlServerDataAdapter((SqlCommand)cmd); XORM.CBase.Data.Common.DataTable dt = new XORM.CBase.Data.Common.DataTable(); adp.Fill(dt); this.Close(); return(dt); } catch (Exception e) { this.Close(); throw e; } }
public Common.DataTable ExecTextDataTable(string SQLText, params object[] cmdParams) { MySqlCommand cmd = new MySqlCommand(); try { CommonPreCmd(SQLText, cmd, null, CommandType.Text, cmdParams); MySqlDataAdapter da = new MySqlDataAdapter(cmd); Common.DataTable dt = new Common.DataTable(); da.Fill(dt); cmd.Parameters.Clear(); this.Close(); return(dt); } catch (Exception e) { this.Close(); throw e; } }
public XORM.CBase.Data.Common.DataTable ExecProcDataTable(string ProcName, params object[] cmdParams) { SqlCommand cmd = new SqlCommand(); try { CommonPreCmd(ProcName, cmd, null, CommandType.StoredProcedure, cmdParams); SqlServerDataAdapter da = new SqlServerDataAdapter(cmd); XORM.CBase.Data.Common.DataTable dt = new XORM.CBase.Data.Common.DataTable(); da.Fill(dt); cmd.Parameters.Clear(); this.Close(); return(dt); } catch (Exception e) { this.Close(); throw e; } }
/// <summary> /// 查询单条记录 /// </summary> /// <param name="EWhere">动态属性,如:exOb.IsDel=0</param> /// <param name="UseReadonlySource">是否实用只读数据源,true-是,false-否</param> /// <returns></returns> public static T Get(ExpandoObject EWhere, bool UseReadonlySource = true) { List <object> SqlParams = new List <object>(); string SqlText = SqlGenerator.SQLBUILDER_Get(DB.ORM_TabInfo, EWhere, out SqlParams); Data.DBHelper db = new Data.DBHelper(DB.ORM_TabInfo.ORMConnectionMark, UseReadonlySource); Data.Common.DataTable dt = db.ExecTextDataTable(SqlText, SqlParams); if (dt != null && dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; DynamicBuilder <T> DyBuilder = DynamicBuilder <T> .CreateBuilder(dr); T Model = DyBuilder.Build(dr); ModelBase <T> TempModel = Model as ModelBase <T>; TempModel.ModifiedColumns.Clear(); return(Model); } else { return(new T()); } }
/// <summary> /// 查询结果整合为结果对象 /// </summary> /// <param name="QueryObj"></param> /// <param name="DT"></param> /// <returns></returns> private static List <QueryResult> BuildQueryResult(Query QueryObj, Data.Common.DataTable DT) { List <QueryResult> jlist = new List <QueryResult>(); if (DT != null && DT.Rows.Count > 0) { foreach (DataRow dr in DT.Rows) { QueryResult jr = new QueryResult(); foreach (string tabSName in QueryObj.OutTabList) { Data.Common.DataTable TempDT = new Data.Common.DataTable(); foreach (DataColumn dc in DT.Columns) { if (dc.ColumnName.StartsWith(tabSName + "_")) { TempDT.Columns.Add(dc.ColumnName.Substring((tabSName + "_").Length)); } } DataRow TempDr = TempDT.NewRow(); foreach (DataColumn dc in TempDT.Columns) { TempDr[dc.ColumnName] = dr[tabSName + "_" + dc.ColumnName]; } TempDT.Rows.Add(TempDr); if (!jr.TabSNameDic.ContainsKey(QueryObj.TabClassDic[tabSName])) { jr.TabSNameDic.Add(QueryObj.TabClassDic[tabSName], new List <string>()); } jr.RowTabDic.Add(tabSName, TempDr); jr.TabSNameDic[QueryObj.TabClassDic[tabSName]].Add(tabSName); } jlist.Add(jr); } } return(jlist); }
/// <summary> /// 分页查询数据 /// </summary> /// <param name="QueryObj"></param> /// <param name="sqlSort"></param> /// <param name="RecordCount"></param> /// <param name="PageIndex"></param> /// <param name="PageSize"></param> /// <param name="UseReadOnlyDataSource"></param> /// <returns></returns> public static List <QueryResult> List(this Query QueryObj, string sqlSort, out long RecordCount, int PageIndex = 1, int PageSize = 20, bool UseReadOnlyDataSource = true) { RecordCount = 0; QueryObj.PageSort = sqlSort; QueryObj.MyCmd.CommandText = QueryObj.SQLTEXT_PAGE; QueryObj.MyCmd.Parameters.Add("@SI", SqlDbType.Int).Value = PageIndex * PageSize - PageSize + 1; QueryObj.MyCmd.Parameters.Add("@EI", SqlDbType.Int).Value = PageIndex * PageSize; DataSet DS = new DBHelper(QueryObj.ORM_TabInfo.ORMConnectionMark, UseReadOnlyDataSource).ExecDataSet(QueryObj.MyCmd); List <QueryResult> jlist = new List <QueryResult>(); if (DS != null && DS.Tables.Count == 2) { Data.Common.DataTable DT = DS.Tables[0]; try { long.TryParse(DS.Tables[1].Rows[0][0].ToString(), out RecordCount); } catch { } jlist = BuildQueryResult(QueryObj, DT); } return(jlist); }