コード例 #1
0
ファイル: Conexion.cs プロジェクト: kevinwagner96/GD2019
 //Recibe el 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 int Insertar(string tabla, Dictionary <string, object> data)
 {
     try
     {
         string comandoString = string.Copy(comandoInsert) + tabla + " (";
         data.Keys.ToList().ForEach(k => comandoString += k + ", ");
         comandoString = comandoString.Substring(0, comandoString.Length - 2) + ") VALUES (";
         data.Keys.ToList().ForEach(k => comandoString += "@" + k + ", ");
         comandoString = comandoString.Substring(0, comandoString.Length - 2) + "); SELECT SCOPE_IDENTITY();";
         PinkieLogger.logInsert(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;
                 foreach (KeyValuePair <string, object> entry in data)
                 {
                     command.Parameters.AddWithValue("@" + entry.Key, entry.Value);
                 }
                 return(Convert.ToInt32(command.ExecuteScalar()));
             }
         }
     }
     catch (SqlException ex)
     {
         PinkieLogger.logExcepcion(ex);
         return(-1);
     }
 }