Run 2 querys or more

2

I would like to execute 2 queries in C # when the button is clicked, but I can only execute one.

public static void excluireventos()
{
    string connectionString = Conn.tank();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        string query = string.Format("TRUNCATE TABLE Active" + "TRUNCATE TABLE Active_Number");
        using (SqlCommand cmd = new SqlCommand(query,connection))
        {
            cmd.ExecuteNonQuery();
            Globals.UpdateLogs("Todos os eventos foram excluidos com exito!");
        }
    }
}

The two queries are TRUNCATE , if I put one will usually more if I put two of the error in the program and date.

How can I perform 2 queries or more?

    
asked by anonymous 15.11.2016 / 21:35

1 answer

1

You are only executing a query , you can execute more than one command in it, as long as they are separated by a semicolon:

public static void excluireventos() {
     using (var connection = new SqlConnection(Conn.tank())) {
         connection.Open();
         using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE Active; TRUNCATE TABLE Active_Number", connection)) {
             cmd.ExecuteNonQuery();
             Globals.UpdateLogs("Todos os eventos foram excluidos com exito!");
         }
    }
}

I gave a simplified one in the code.

    
15.11.2016 / 21:44