/// <summary> /// 是否存在记录 /// </summary> /// <returns></returns> public EntityResult Exists() { string sql = "SELECT * FROM " + this.GetType().Name + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; EntityResult result = GetEntityResult(SMT.DataProvider.ExistsRecord(sql)); int n = result.RecordCount; if (n > 0) { eb = null; result.RecordCount = n; } else { result.RecordCount = 0; } return result; }
/// <summary> /// 更新 /// </summary> /// <returns></returns> public EntityResult Update() { #region sql语句 //Result result = new Result(); //eb = this; //string sql=""; //Type type=this.GetType(); //foreach (PropertyInfo p in this.GetType().GetProperties()) // { // //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); // //this.GetType().GetProperties()[3].Name // if (has.Contains(p.Name.ToUpper())) // { // if (PrimaryKeyName.ToUpper() != p.Name.ToUpper()) // { // sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',"; // } // } // } //string exeSql="UPDATE "+this.GetType().Name+" SET "+sql.TrimEnd(',')+" WHERE "+ PrimaryKeyName.ToUpper() + "='" + PrimaryKey + "'"; //result.Sql = exeSql; //int n = SMT.DataProvider.ExecuteSQL(exeSql); //if (n > 0) //{ // result.RecordCount = n; //} //else //{ // result.RecordCount = 0; //} //return result; #endregion #region 参数化 eb = this; string sql = ""; Type type = this.GetType(); string name = ""; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); //this.GetType().GetProperties()[3].Name if (has.Contains(p.Name.ToUpper())) { if (PrimaryKeyName.ToUpper() != p.Name.ToUpper()) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',"; name += p.Name.ToUpper() + "=:" + p.Name.ToUpper() + ","; i++; } } } sql = "UPDATE " + this.GetType().Name + " SET " + sql.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; SMT.DataProvider.AddParameters(i, PrimaryKeyName.ToUpper(), PrimaryKeyValue); string exeSql = "UPDATE " + this.GetType().Name + " SET " + name.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "=:" + PrimaryKeyName.ToUpper() + ""; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql)); result.Sql = sql; int n = result.RecordCount; if (n > 0) { result.RecordCount = n; } else { result.RecordCount = 0; } return result; #endregion }
/// <summary> /// 增加 /// </summary> /// <returns></returns> public EntityResult Add() { eb = this; Type type = this.GetType(); //string name = "("; //string value = "("; //foreach (PropertyInfo p in this.GetType().GetProperties()) //{ // //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); // //this.GetType().GetProperties()[3].Name // if (has.Contains(p.Name.ToUpper())) // { // name += p.Name.ToUpper() + ","; // value +="'"+ p.GetValue(eb, null) +"',"; // } //} string name = "("; string value = "("; string name2 = "("; string value2 = "("; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { if (has.Contains(p.Name.ToUpper())) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); name += p.Name.ToUpper() + ","; value += ":" + p.Name.ToUpper() + ","; name2 += p.Name.ToUpper() + ","; value2 += "'" + p.GetValue(eb, null) + "',"; i++; } } if (has.Count != i) {//数据表的字段与实体定义的字段属表有不一样的,重新分配 SMT.DataProvider.ClearParameters(); name = "("; value = "("; name2 = "("; value2 = "("; SMT.DataProvider.CreateParameters(i); i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { if (has.Contains(p.Name.ToUpper())) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); name += p.Name.ToUpper() + ","; value += ":" + p.Name.ToUpper() + ","; name2 += p.Name.ToUpper() + ","; value2 += "'" + p.GetValue(eb, null) + "',"; i++; } } } string Sql = "INSERT INTO " + this.GetType().Name + " " + name2.TrimEnd(',') + ")VALUES" + value2.TrimEnd(',') + ")"; string exeSql = "INSERT INTO " + this.GetType().Name + " " + name.TrimEnd(',') + ")VALUES" + value.TrimEnd(',') + ")"; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql)); result.Sql = Sql; int n = result.RecordCount; if (n> 0) { result.RecordCount = n; } else { result.RecordCount = 0; } return result; }
/// <summary> /// 删除 /// </summary> /// <param name="childTableName">从表名称如是多个从表,以逗号隔开</param> /// <param name="primaryKeyName">主健字段名称</param> /// <returns></returns> public EntityResult Delete(string childTableName, string primaryKeyName) { List<string> sqllist=new List<string>(); if (childTableName.IndexOf(',') > 0) { string[] sqls=childTableName.Split(','); for (int i = 0; i < sqls.Length; i++) { string childsql = "DELETE FROM " + sqls[i] + " WHERE " + primaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; sqllist.Add(childsql); } } else { string childsql = "DELETE FROM " + childTableName + " WHERE " + primaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; sqllist.Add(childsql); } string mainsql = "DELETE FROM " + this.GetType().Name + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; sqllist.Add(mainsql); EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteByTransaction(sqllist)); int n = result.RecordCount; if (n > 0) { eb = null; result.RecordCount = n; } else { result.RecordCount = 0; } return result; }
/// <summary> /// 更改(通过事务,必须在BeginTransaction()和 CommitTransaction()二个方法中间) /// </summary> public EntityResult UpdateByTransaction(IDbCommand idbconn) { #region 参数化 eb = this; string sql = ""; Type type = this.GetType(); string name = ""; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); //this.GetType().GetProperties()[3].Name if (has.Contains(p.Name.ToUpper())) { if (PrimaryKeyName.ToUpper() != p.Name.ToUpper()) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',"; name += p.Name.ToUpper() + "=:" + p.Name.ToUpper() + ","; i++; } } } sql = "UPDATE " + this.GetType().Name + " SET " + sql.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; SMT.DataProvider.AddParameters(i, PrimaryKeyName.ToUpper(), PrimaryKeyValue); string exeSql = "UPDATE " + this.GetType().Name + " SET " + name.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "=:" + PrimaryKeyName.ToUpper() + ""; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQLByTransaction(idbconn, exeSql)); result.Sql = sql; return result; #endregion }
//static IDbConnection idbconn; ///// <summary> ///// 开始事务 ///// </summary> //public void BeginTransaction() //{ // idbconn= SMT.DataProvider.BeginTransaction(); //} ///// <summary> ///// 提交事务 ///// </summary> //public bool CommitTransaction() //{ // return SMT.DataProvider.CommitTransaction(); //} #region 事务增加\更新\删除 /// <summary> /// 增加(通过事务,必须在BeginTransaction()和 CommitTransaction()二个方法中间) /// </summary> public EntityResult AddByTransaction(IDbCommand idbconn) { eb = this; Type type = this.GetType(); string name = "("; string value = "("; string name2 = "("; string value2 = "("; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { if (has.Contains(p.Name.ToUpper())) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); name += p.Name.ToUpper() + ","; value += ":" + p.Name.ToUpper() + ","; name2 += p.Name.ToUpper() + ","; value2 += "'" + p.GetValue(eb, null) + "',"; i++; } } string Sql = "INSERT INTO " + this.GetType().Name + " " + name2.TrimEnd(',') + ")VALUES" + value2.TrimEnd(',') + ")"; string exeSql = "INSERT INTO " + this.GetType().Name + " " + name.TrimEnd(',') + ")VALUES" + value.TrimEnd(',') + ")"; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQLByTransaction(idbconn, exeSql)); result.Sql = Sql; return result; }
/// <summary> /// 增加 /// </summary> /// <returns></returns> public EntityResult Add() { eb = this; Type type = this.GetType(); //string name = "("; //string value = "("; //foreach (PropertyInfo p in this.GetType().GetProperties()) //{ // //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); // //this.GetType().GetProperties()[3].Name // if (has.Contains(p.Name.ToUpper())) // { // name += p.Name.ToUpper() + ","; // value +="'"+ p.GetValue(eb, null) +"',"; // } //} string name = "("; string value = "("; string name2 = "("; string value2 = "("; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { if (has.Contains(p.Name.ToUpper())) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); name += p.Name.ToUpper() + ","; value += ":" + p.Name.ToUpper() + ","; name2 += p.Name.ToUpper() + ","; value2 += "'" + p.GetValue(eb, null) + "',"; i++; } } if (has.Count != i) {//数据表的字段与实体定义的字段属表有不一样的,重新分配 SMT.DataProvider.ClearParameters(); name = "("; value = "("; name2 = "("; value2 = "("; SMT.DataProvider.CreateParameters(i); i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { if (has.Contains(p.Name.ToUpper())) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); name += p.Name.ToUpper() + ","; value += ":" + p.Name.ToUpper() + ","; name2 += p.Name.ToUpper() + ","; value2 += "'" + p.GetValue(eb, null) + "',"; i++; } } } string Sql = "INSERT INTO " + this.GetType().Name + " " + name2.TrimEnd(',') + ")VALUES" + value2.TrimEnd(',') + ")"; string exeSql = "INSERT INTO " + this.GetType().Name + " " + name.TrimEnd(',') + ")VALUES" + value.TrimEnd(',') + ")"; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql)); result.Sql = Sql; int n = result.RecordCount; if (n > 0) { result.RecordCount = n; } else { result.RecordCount = 0; } return(result); }
/// <summary> /// 更新 /// </summary> /// <returns></returns> public EntityResult Update() { #region sql语句 //Result result = new Result(); //eb = this; //string sql=""; //Type type=this.GetType(); //foreach (PropertyInfo p in this.GetType().GetProperties()) // { // //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); // //this.GetType().GetProperties()[3].Name // if (has.Contains(p.Name.ToUpper())) // { // if (PrimaryKeyName.ToUpper() != p.Name.ToUpper()) // { // sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',"; // } // } // } //string exeSql="UPDATE "+this.GetType().Name+" SET "+sql.TrimEnd(',')+" WHERE "+ PrimaryKeyName.ToUpper() + "='" + PrimaryKey + "'"; //result.Sql = exeSql; //int n = SMT.DataProvider.ExecuteSQL(exeSql); //if (n > 0) //{ // result.RecordCount = n; //} //else //{ // result.RecordCount = 0; //} //return result; #endregion #region 参数化 eb = this; string sql = ""; Type type = this.GetType(); string name = ""; if (has == null) { has = DataBaseType.GetTableColumn(this.GetType().Name); } SMT.DataProvider.CreateParameters(has.Count); int i = 0; foreach (PropertyInfo p in this.GetType().GetProperties()) { //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null)); //this.GetType().GetProperties()[3].Name if (has.Contains(p.Name.ToUpper())) { if (PrimaryKeyName.ToUpper() != p.Name.ToUpper()) { SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null)); sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',"; name += p.Name.ToUpper() + "=:" + p.Name.ToUpper() + ","; i++; } } } sql = "UPDATE " + this.GetType().Name + " SET " + sql.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'"; SMT.DataProvider.AddParameters(i, PrimaryKeyName.ToUpper(), PrimaryKeyValue); string exeSql = "UPDATE " + this.GetType().Name + " SET " + name.TrimEnd(',') + " WHERE " + PrimaryKeyName.ToUpper() + "=:" + PrimaryKeyName.ToUpper() + ""; EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql)); result.Sql = sql; int n = result.RecordCount; if (n > 0) { result.RecordCount = n; } else { result.RecordCount = 0; } return(result); #endregion }