コード例 #1
0
ファイル: Conexion.cs プロジェクト: kevinwagner96/GD2019
 //Recibe el id de la fila, nombre de la tabla sacada de Conexion.Tabla, y
 //un diccionario con el par ("nombre de columna en BD", dato a insertar
 //retorna true si se pudo realizar, false si fallo
 public bool Modificar(int pk, string tabla, Dictionary <string, object> data)
 {
     try
     {
         string comandoString = string.Copy(comandoUpdate) + tabla + " SET ";
         foreach (KeyValuePair <string, object> entry in data)
         {
             comandoString += entry.Key + " = @" + entry.Key + ", ";
         }
         comandoString = comandoString.Substring(0, comandoString.Length - 2) + " WHERE id = @id";
         PinkieLogger.logUpdate(comandoString, data);
         using (SqlConnection sqlConnection = new SqlConnection(conectionString))
         {
             sqlConnection.Open();
             using (SqlCommand command = new SqlCommand())
             {
                 command.Connection  = sqlConnection;
                 command.CommandType = CommandType.Text;
                 command.CommandText = comandoString;
                 command.Parameters.AddWithValue("@id", pk);
                 foreach (KeyValuePair <string, object> entry in data)
                 {
                     command.Parameters.AddWithValue("@" + entry.Key, entry.Value);
                 }
                 command.ExecuteNonQuery();
             }
         }
     }
     catch (SqlException ex)
     {
         PinkieLogger.logExcepcion(ex);
         return(false);
     }
     return(true);
 }