/* ------------------------------------------- */ #region [ Insert method ] /// <summary> /// Insert the record to table with table_name with given fields. /// </summary> /// <param name="table_name">table name</param> /// <param name="fields">column and values</param> /// <returns>Returns exec result of Insert.</returns> public virtual int Insert(string table_name, Hashmap fields) { int result = -100; try { if (string.IsNullOrWhiteSpace(table_name)) { throw new Exception("Table Name can not be null or empty."); } if (table_name.Contains("drop") || table_name.Contains("--")) { throw new Exception( "Table Name can not be contain restricted characters and text."); } if (fields == null) { throw new Exception( "Column list can not be null."); } if (fields.IsEmpty()) { throw new Exception( "Column list can not be empty."); } QueryFormat qf = new QueryFormat(QueryTypes.Insert); QueryAdds adds = new QueryAdds(this.conn_type); string query = "", cols = "", vals = ""; Hashmap h = new Hashmap(); foreach (var field in fields.Keys()) { cols = string.Format("{0}, {1}{2}{3}", cols, adds.Prefix, field, adds.Suffix); vals = string.Format("{0}, {1}{2}", cols, adds.ParameterPrefix, field); h.Set(string.Format("{0}{1}", adds.ParameterPrefix, field), fields.Get(field)); } cols = cols.TrimStart(',').TrimStart(); vals = vals.TrimStart(',').TrimStart(); query = string.Format(qf.Format, table_name, cols, vals); result = this.Execute(query, CommandType.Text, h); } catch (Exception) { throw; } return(result); }
/// <summary> /// Update records with given parameters. /// </summary> /// <param name="table_name">table name</param> /// <param name="where_column">id column name, if null or empty value will be "id"</param> /// <param name="where_value">id column value</param> /// <param name="fields">column and values</param> /// <returns>Returns exec result of Update.</returns> public virtual int Update(string table_name, string where_column, object where_value, Hashmap fields) { int result = -1; try { if (string.IsNullOrWhiteSpace(table_name)) { throw new Exception("Table Name can not be null or empty."); } if (table_name.Contains("drop") || table_name.Contains("--")) { throw new Exception( "Table Name can not be contain restricted characters and text."); } if (fields == null) { throw new Exception( "Column list can not be null."); } if (fields.IsEmpty()) { throw new Exception( "Column list can not be empty."); } if (string.IsNullOrWhiteSpace(where_column)) { throw new Exception("Table Name can not be null or empty."); } if (where_column.Contains("drop") || where_column.Contains("--")) { throw new Exception( "Table Name can not be contain restricted characters and text."); } QueryFormat qf = new QueryFormat(QueryTypes.Update); QueryAdds qo = new QueryAdds(this.conn_type); string query = "", cols = "", vals = ""; //query = qf.Format; Hashmap h = new Hashmap(); FreeParameter fp; foreach (var field in fields.Keys()) { cols = string.Format("{0}, {1}{2}{3}={4}{2}", cols, qo.Prefix, field, qo.Suffix, qo.ParameterPrefix); fp = new FreeParameter { Name = string.Format("{0}{1}", qo.ParameterPrefix, field), Value = fields.Get(field) }; h.Set(string.Format("{0}{1}", qo.ParameterPrefix, field), fields.Get(field)); } h.Set(string.Format("{0}{1}", qo.ParameterPrefix, where_column), where_value); cols = cols.TrimStart(',').TrimStart(); vals = string.Format("{0}{1}{2}={3}{1}", qo.Prefix, where_column, qo.Suffix, qo.ParameterPrefix); query = string.Format(qf.Format, table_name, cols, vals); result = this.ExecuteQuery(query, h); } catch (Exception) { throw; } return(result); }