/// <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.Count == 0) { 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 }