public MObj Load(MObj obj, string sql, params object[] parms) { SqlConnection con = ConnectionPool.GetConnection(); try { parms = GetParamList(parms); DateTime begtime = DateTime.Now; SqlCommand com = new SqlCommand(sql, con); for (int i = 0; i < parms.Length - 1; i += 2) { if (parms[i] != null) { com.Parameters.Add(new SqlParameter(parms[i].ToString(), PrepareParam(parms[i + 1]))); } } using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { SetObjValues(obj, rdr); break; } } MainTrace.Add(TraceType.Sql, $"QUERY => {com.CommandText}"); } finally { ConnectionPool.FreeConnection(con); } return(obj); }
public T Query <T>(string sql, params object[] parms) { object r = Query(sql, parms); if (r == null) { return(default(T)); } return((T)MObj.GetTypeValue(typeof(T), r)); }
public void SetObjValues(MObj obj, XmlNode xrow) { if (xrow != null) { foreach (XmlNode xnode in xrow.ChildNodes) { LockUpdates++; obj.SetStringValue(xnode.Name, xnode.InnerText, false); LockUpdates--; } } }
public void CopyValues(MObj source) { if (source != null) { foreach (PropertyInfo pi in this.GetType().GetRuntimeProperties()) { if (pi.CanWrite && pi.Name != "Model" && pi.Name != "LockUpdates") { SetValue(pi.Name, source.GetValue(pi.Name)); } } } }
public void SetObjValues(MObj obj, SqlDataReader rdr) { for (int i = 0; i < rdr.FieldCount; i++) { string name = rdr.GetName(i).TrimEnd(); object value = rdr.GetValue(i); if (value is DBNull) { value = null; } LockUpdates++; obj.SetValue(name, value); LockUpdates--; } }
public void SelectFromXML(IList list, Type t, XmlNode xroot, object id) { if (xroot != null) { try { bool f = MainDic.ContainsKey(t); { foreach (XmlNode xrow in xroot.ChildNodes) { if (xrow.Name == "row") { if (id == null || id.ToString() == "" || id.ToString().ToLower() == XFunc.GetAttr(xrow, "id", "")) { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, xrow); if (list != null) { list.Add(obj); } if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } } } } } } finally { } } }
public IList Select(IList list, Type t, string sql, params object[] parms) { SqlConnection con = ConnectionPool.GetConnection(); try { parms = GetParamList(parms); int allCount = -1; IPaginationList plist = null; if (list is IPaginationList) { plist = (IPaginationList)(list); } DateTime begtime = DateTime.Now; SqlCommand com = new SqlCommand(sql, con); for (int i = 0; i < parms.Length - 1; i += 2) { if (parms[i] != null) { com.Parameters.Add(new SqlParameter(parms[i].ToString(), PrepareParam(parms[i + 1]))); } } // Для PaginationList select выполняем в 2 этапа сначала считаем кол-во а потом сам select c добавленным top if (plist != null) { if (plist.Top != "") { string sql1 = com.CommandText; int i = sql1.IndexOf('*'); int j = sql1.ToLower().IndexOf("order by"); if (i > 0 && (j < 0 || i < j)) { com.CommandText = sql1.Remove(j).Remove(i, 1).Insert(i, "count(*)"); allCount = Convert.ToInt32(com.ExecuteScalar()); com.CommandText = sql1.Insert(i, plist.Top + " "); } } } bool f = MainDic.ContainsKey(t); using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { if (plist != null) { if ((plist.AllCount >= plist.StartPos || plist.StartPos == -1) && plist.AllCount < plist.StartPos + plist.PageSize || plist.StartPos == -1 || plist.PageSize == -1) { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, rdr); if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } if (list != null) { list.Add(obj); } } plist.AllCount++; } else { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, rdr); if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } if (list != null) { list.Add(obj); } } } } if (plist != null && allCount >= 0) { plist.AllCount = allCount; } MainTrace.Add(TraceType.Sql, $"SELECT => {com.CommandText}"); } finally { ConnectionPool.FreeConnection(con); } return(list); }