Agregar columna a tabla mediante código c# en base de datos access

Hola amigos, aquí les dejo un ejemplo de como modificar una tabla de base de datos access utilizando código C#:

public static void ScriptActualizacion000(){
    int RowCount = 0;
    OleDbConnection cnn = new OleDbConnection(clsMain.CnnStr);//Cadena de Conexión
    try{
        cnn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = cnn;
        //AGREGAR COLUMNA A TABLA
        RowCount = 0;
        DataTable schema = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "VENTA", null });
        foreach (DataRow row in schema.Rows) { 
            if (row["COLUMN_NAME"].ToString() == "TOTAL") 
             RowCount = 1;  
        }
        if (RowCount == 0){
            cmd.CommandText = "ALTER TABLE VENTA ADD TOTAL DOUBLE";
            cmd.ExecuteNonQuery();
        }
        RowCount = 0;
    }
    catch (Exception ex) { throw ex; }
    finally { cnn.Close(); }
}

No hay comentarios:

Publicar un comentario