void IDB.Insert(object obj) { System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.Connection = conn; cmd.Transaction = tran; cmd.CommandTimeout = 8000; // string sql = ""; string fields = ""; string values = ""; foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { if (fields == "") { fields += p.Name; values += "@" + p.Name; } else { fields += "," + p.Name; values += "," + "@" + p.Name; } } sql = "insert into " + ReflectionHelper.GetDataTableNameByModel(obj) + "(" + fields + ")values(" + values + ")"; cmd.CommandText = sql; cmd.Parameters.AddRange(ReflectionHelper.ModelToSqlParameters(obj)); // cmd.ExecuteScalar(); }
void IDB.Update(object obj, string key_fields) { System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.Connection = conn; cmd.Transaction = tran; cmd.CommandTimeout = 8000; // string sql = ""; string fields = ""; foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { int flag = 0; foreach (string f in key_fields.Split(',')) { if (p.Name.ToLower() == f.ToLower()) { flag = 1; } } if (flag == 1) { continue; } if (fields == "") { fields += p.Name + "=" + "@" + p.Name; } else { fields += "," + p.Name + "=" + "@" + p.Name; } } string filter = ""; if (key_fields.Contains(",") == false) { filter = key_fields + "=" + "@" + key_fields; } else { foreach (string field in key_fields.Split(',')) { if (filter == "") { filter += field + "=" + "@" + field; } else { filter += " and " + field + "=" + "@" + field; } } } sql = "update " + ReflectionHelper.GetDataTableNameByModel(obj) + " set " + fields + " where " + filter; cmd.CommandText = sql; cmd.Parameters.AddRange(ReflectionHelper.ModelToSqlParameters(obj)); // cmd.ExecuteScalar(); }
void IDB.Update(object obj, string key_fields) { System.Data.SqlClient.SqlConnection conn = this.GetNewConn(); try { System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 8000; // string sql = ""; string fields = ""; foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { if (fields == "") { fields += p.Name + "=" + "@" + p.Name; } else { fields += "," + p.Name + "=" + "@" + p.Name; } } string filter = ""; if (key_fields.Contains(",") == false) { filter = key_fields + "=" + "@" + key_fields; } else { foreach (string field in key_fields.Split(',')) { if (filter == "") { filter += field + "=" + "@" + field; } else { filter += " and " + field + "=" + "@" + field; } } } sql = "update " + ReflectionHelper.GetDataTableNameByModel(obj) + " set " + fields + " where " + filter; cmd.CommandText = sql; cmd.Parameters.AddRange(ReflectionHelper.ModelToSqlParameters(obj)); // conn.Open(); cmd.ExecuteScalar(); } catch (Exception e) { throw; } finally { conn.Close(); } }
void IDB.Insert(object obj, string without_fields) { System.Data.SqlClient.SqlConnection conn = this.GetNewConn(); try { System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 8000; // string sql = ""; string fields = ""; string values = ""; foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { int flag = 0; foreach (string f in without_fields.Split(',')) { if (p.Name.ToLower() == f.ToLower()) { flag = 1; } } if (flag == 1) { continue; } if (fields == "") { fields += p.Name; values += "@" + p.Name; } else { fields += "," + p.Name; values += "," + "@" + p.Name; } } sql = "insert into " + ReflectionHelper.GetDataTableNameByModel(obj) + "(" + fields + ")values(" + values + ")"; cmd.CommandText = sql; cmd.Parameters.AddRange(ReflectionHelper.ModelToSqlParameters(obj)); // conn.Open(); cmd.ExecuteScalar(); } catch (Exception e) { throw; } finally { conn.Close(); } }
void IDB.Insert(object obj) { MySql.Data.MySqlClient.MySqlConnection conn = this.GetNewConn(); try { MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 8000; // string sql = ""; string fields = ""; string values = ""; foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { if (fields == "") { fields += p.Name; values += "@" + p.Name; } else { fields += "," + p.Name; values += "," + "@" + p.Name; } } sql = "insert into " + ReflectionHelper.GetDataTableNameByModel(obj) + "(" + fields + ")values(" + values + ")"; cmd.CommandText = sql; cmd.Parameters.AddRange(ReflectionHelper.ModelToSqlParameters(obj)); // conn.Open(); cmd.ExecuteScalar(); } catch (Exception) { throw; } finally { conn.Close(); } }