public M ToObj <M>(Func <IDataReader, M> func, string sql = null, bool issp = false) { try { if (string.IsNullOrEmpty(sql)) { sql = GetSql(); } if (OrmGlobal.isrecord) { Record(sql); } M t = default(M); using (var rd = db.ExectueReader(sql, ps, false)) { #if DEBUG if (rd == null) { Clear(); return(default(M)); } #endif t = func(rd); rd.Close(); } Clear(); return(t); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public object Insert(T m) { try { StringBuilder fields = new StringBuilder(); StringBuilder values = new StringBuilder(); Guid gd = Guid.Empty; string tp = string.Empty; object o = null; var getmth = GetMethod; foreach (var n in Fields.Values) { if (n.IsExcludeColumn || n.IsExcludeDelegate) { continue; } if (n.IsKey) { if (n.FieldType.Equals(typeof(Guid))) { gd = Guid.NewGuid(); fields.Append(Quote(n.Name) + ","); tp = db.ParStr(n.Name); values.Append(tp + ","); ps.Add(db.Cp(tp, gd)); } continue; } o = getmth.Invoke(m, n.Name.ToLower()); if (o == null) { continue; } fields.Append(Quote(n.Name) + ","); tp = db.ParStr(n.Name); values.Append(tp + ","); ps.Add(db.Cp(tp, o)); } if (fields.Length > 0) { fields.Length--; } if (values.Length > 0) { values.Length--; } tp = "INSERT INTO " + TableName + "(" + fields.ToString() + ")VALUES(" + values.ToString() + ") " + (gd != Guid.Empty?string.Empty:db.GetIdStr); if (OrmGlobal.isrecord) { Record(tp); } object a = db.ExectueScalar(tp, ps, false); Clear(); return(gd != Guid.Empty?gd:a); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public void Import(DbDataReader dt, Action <object> act = null) { try { db.Import(dt, TableName, act); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public int Update(T m) { try { StringBuilder sb = new StringBuilder(); sb.Append("UPDATE " + TableName + " SET "); object o = null; int j = 0; var getmth = GetMethod; IDataParameter p = null; foreach (var n in Fields.Values)//为了保证参数顺序,参数用了Insert { if (n.IsExcludeColumn || n.IsExcludeDelegate) { continue; } o = getmth.Invoke(m, n.Name.ToLower()); if (o == null) { continue; } if (n.IsKey) { where.Append((where.Length > 0 ? " AND " : string.Empty) + Quote(n.Name) + "=" + db.ParStr(n.Name)); p = db.Cp(db.ParStr(n.Name), o); continue; } sb.Append(Quote(n.Name) + "=" + db.ParStr(n.Name) + ","); ps.Insert(j, db.Cp(db.ParStr(n.Name), o)); j++; } if (p != null) { ps.Add(p); } if (sb.Length > 0) { sb.Length--; } if (where.Length > 0) { sb.Append(" WHERE " + where); } var sql = sb.ToString(); if (OrmGlobal.isrecord) { Record(sql); } int i = db.ExecuteQuery(sql, ps, false); Clear(); return(i); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public DataTable ToDataTable(string sql, bool issp = false) { try { if (OrmGlobal.RecordLog) { Record(sql); } var tp = db.GetDataTable(sql, ps, issp); Clear(); return(tp); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public int Update(Dictionary <string, object> dic) { try { StringBuilder sb = new StringBuilder(); sb.Append("UPDATE " + TableName + " SET "); int j = 0; IDataParameter p = null; foreach (var n in dic) { if (n.Key == Key) { where.Append((where.Length > 0 ? " AND " : string.Empty) + Quote(n.Key) + "=" + db.ParStr(n.Key)); p = db.Cp(db.ParStr(n.Key), n.Value); continue; } sb.Append(Quote(n.Key) + "=" + db.ParStr(n.Key) + ","); ps.Insert(j, db.Cp(db.ParStr(n.Key), n.Value)); j++; } if (p != null) { ps.Add(p); } if (sb.Length > 0) { sb.Length--; } if (where.Length > 0) { sb.Append(" WHERE " + where); } var sql = sb.ToString(); if (OrmGlobal.isrecord) { Record(sql); } int i = db.ExecuteQuery(sql, ps, false); Clear(); return(i); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public int Count() { try { string sql = "SELECT COUNT(*) FROM " + TableName + (where.Length > 0 ? " WHERE " + where : string.Empty); if (OrmGlobal.RecordLog) { Record(sql); } int i = Convert.ToInt32(db.ExectueScalar(sql, ps, false)); Clear(); return(i); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public int Delete() { try { string sql = "DELETE FROM " + TableName + (where.Length > 0 ? " WHERE " + where : string.Empty); if (OrmGlobal.isrecord) { Record(sql); } int i = db.ExecuteQuery(sql, ps, false); Clear(); return(i); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }
public int Update(string str) { try { string tp = "UPDATE " + TableName + " SET " + str + (where.Length > 0 ? " WHERE " + where : string.Empty); if (OrmGlobal.isrecord) { Record(tp); } int i = db.ExecuteQuery(tp, ps, false); Clear(); return(i); } catch { OrmGlobal.DoErr(sqlinfo.ToString()); throw; } }