public TEntity Load(object id) { IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); string tableName = entityType.Name.ToLower(); dbCommand.CommandText = string.Format(selectSql, tableName, idPropertyName.ToLower()); DbCommandHelper.AddParameter(dbCommand, "id", id); IDataReader dataReader = dbCommand.ExecuteReader(); dataReader.Read(); TEntity entity = Activator.CreateInstance <TEntity>(); foreach (string propertyName in entityPropertyNames) { object value = dataReader[propertyName.ToLower()]; if (value == DBNull.Value) { value = null; } entityType.GetProperty(propertyName).SetValue(entity, value); } dataReader.Close(); return(entity); }
public void Delete(object id) { string tableName = entityType.Name.ToLower(); IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); dbCommand.CommandText = string.Format(deleteSql, tableName, idPropertyName.ToLower()); DbCommandHelper.AddParameter(dbCommand, "id", id); dbCommand.ExecuteNonQuery(); }
private static void update(Categoria categoria) { IDbCommand dbCommand = App.Instance.Connection.CreateCommand(); dbCommand.CommandText = "update categoria set nombre=@nombre where id = @id"; DbCommandHelper.AddParameter(dbCommand, "id", categoria.Id); DbCommandHelper.AddParameter(dbCommand, "nombre", categoria.Nombre); dbCommand.ExecuteNonQuery(); }
public void insert(TEntity entity) { IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); //insert into categoria (nombre,precio,categoria) values (@nombre, @precio, @categoria) //FORMAS DE REALIZAR INSERT //List<string> fieldsWithoutID = entityPropertyNames.GetRange(1, entityPropertyNames.Count -1); //List<string> fieldsWithoutID = new List<string>(entityPropertyNames); //fieldsWithoutID.RemoveAt(0); List <string> fieldsWithoutId = new List <string>(); List <string> fieldParameterPairs = new List <string>(); for (int index = 1; index < fieldsWithoutId.Count; index++) { fieldsWithoutId.Add(fieldsWithoutId[index]); fieldParameterPairs.Add("@" + entityPropertyNames[index].ToLower()); } //List<string> fieldsWithoutID = new List<string>(); //foreach(string item in entityPropertyNames){ // if (item != "id") // fieldsWithoutID.Add(item); //} //List<string> fieldsWithoutId = entityPropertyNames.FindAll(item => item != "id"); // fieldsWithoutId.ForEach(item => item = "0" + item); //List<string> fieldParameterPairs = entityPropertyNames.FindAll(item => item != "id"); //fieldParameterPairs.ForEach(item => item = "=@" + item); string fieldNamesCsv = string.Join(", ", fieldsWithoutId).ToLower(); string parametersCsv = string.Join(", ", fieldParameterPairs).ToLower(); string tableName = entityType.Name.ToLower(); dbCommand.CommandText = string.Format(insertSql, tableName, fieldNamesCsv, parametersCsv); foreach (string fieldname in fieldsWithoutId) { object value = entityType.GetProperty(fieldname).GetValue(entity); DbCommandHelper.AddParameter(dbCommand, fieldname, value); } dbCommand.ExecuteNonQuery(); //IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); //dbCommand.CommandText = string.Format(insertSql, tableName, fieldNamesCsv); }
private static Categoria load(object id) { IDbCommand dbCommand = App.Instance.Connection.CreateCommand(); dbCommand.CommandText = "select * from categoria where id = @id"; DbCommandHelper.AddParameter(dbCommand, "id", id); IDataReader dataReader = dbCommand.ExecuteReader(); dataReader.Read(); string nombre = (string)dataReader["nombre"]; dataReader.Close(); Categoria categoria = new Categoria(); categoria.Id = Convert.ToInt64(id); categoria.Nombre = nombre; return(categoria); }
protected void Insert(TEntity entity) { IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); List <string> insertFieldNames = new List <string>(); List <string> parameterNames = new List <string>(); insertFieldNames.ForEach(item => parameterNames.Add("@" + item)); string tableName = entityType.Name.ToLower(); string insertFieldNameCsv = string.Join(",", insertFieldNames).ToLower(); string parameterNamesCsv = string.Join(", ", parameterNames).ToLower(); dbCommand.CommandText = string.Format(insertSql, tableName, insertFieldNameCsv, parameterNamesCsv); foreach (string fieldName in insertFieldNames) { object value = entityType.GetProperty(fieldName).GetValue(entity); DbCommandHelper.AddParameter(dbCommand, fieldName, value); } }
protected void insert(TEntity entity) { IDbCommand dbCommand = App.Instance.DbConnection.CreateCommand(); List <string> insertPropertyNames = entityPropertyNames.FindAll(item => item != idPropertyName); List <string> parameterNames = new List <string>(); insertPropertyNames.ForEach(item => parameterNames.Add("@" + item)); string tableName = entityType.Name.ToLower(); string insertFieldNamesCsv = string.Join(", ", insertPropertyNames).ToLower(); string parameterNamesCsv = string.Join(", ", parameterNames).ToLower(); dbCommand.CommandText = string.Format(insertSql, tableName, insertFieldNamesCsv, parameterNamesCsv); //DbCommandHelper.AddParameter(dbCommand, "nombre", categoria.Nombre); foreach (string propertyName in insertPropertyNames) { object value = entityType.GetProperty(propertyName).GetValue(entity); DbCommandHelper.AddParameter(dbCommand, propertyName, value); } dbCommand.ExecuteNonQuery(); }