1

This is the Code i have and it works well when choose one connection string in Combobox

How can i execute same SQL query in multiple connections with one button click not one by one??

public string connstring = "";
        private void cmbSrv_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSrv.SelectedIndex == 0)
            {
                connstring = @"Data Source=tcp:10.1.0.100;Initial Catalog=Database1;User ID=user;Password=pass;MultipleActiveResultSets=true;";
            }
            else if (cmbSrv.SelectedIndex == 1)
            {
                connstring = @"Data Source=tcp:10.0.0.100;Initial Catalog=Database2 ;User ID=user;Password=pass;MultipleActiveResultSets=true;";

            }
        }
private void btnKonfirm_Click(object sender, EventArgs e)
    {
using (SqlConnection connection = new SqlConnection(connstring))

            {
                SqlCommand cmd1 = new SqlCommand();

                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }

                SqlCommand command1 =
                    new SqlCommand("DELETE FROM TABLE1 WHERE ID="+textbox1+"", connection);                    



                command1.ExecuteNonQuery();


                connection.Close();
            }
}
1
  • Check my answer and tell me if something is not clear. Commented Oct 25, 2016 at 11:38

1 Answer 1

3

You have some problems with your code like Sql Injection, you can fixed with parameterized queries.

You are not taking the Text property of your textbox1. Fix your naming and don't use textbox1 for control names. Naming is important, so you can be understand by other programmers. Same is true for your database table, Table1 is not appropriate name.

You don't need to Close() the connection when you are in using block. Using do this automatically. Also when you create an SqlConnection object you can open a connection, there is no need to make if checks about it.

public void ConfirmBtn_Click(object sender, EventArgs e)
{
     string connString1 = "FirstConnectionSTring";
     string connString2 = "SecondConnectionSTring";

     ExecuteNonQuery(connString1);
     ExecuteNonQuery(connString2);
}

public void ExecuteNonQuery(string connString)
{
    using (SqlConnection connection = new SqlConnection(connString))
    {

         connection.Open();
         SqlCommand cmd =
                    new SqlCommand("DELETE FROM TABLE1 WHERE ID=@ID", connection);

         cmd.Parameters.AddWithValue("@ID", textbox1.Text);         

         cmd.ExecuteNonQuery();

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@AlexK. If you don't dispose SqlCommand you will not hurt yourself, there are no resources which should be handle by finalizer in the current implementation of the class. But yes it is a good practise to always Dispose IDisposable objects. One of the reasons is that the implementation can change in the future.
@AlexK. Also after research if you check the Constructor of SqlCommand in reference code you will see that he is calling GC.SuppressFinalize(this); . Reference Code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.