0

I'm using mobile for sending data to a local SQL Server. My question is which is the best method to use? Also with best performance over network.

First method is to create connection string once and to opening-closing connection in each query.

SqlConnection con = new SqlConnection("Data Source = " + MyIp + "; Initial Catalog = xxxx; user id = xxxx; password = xxxx;Connection Timeout=3");

Btn.Click+=delegate
{
    string query="";
    query="Insert into Customers (name) values (dim)";

    con.open();
    SqlCommand cmd = new SqlCommand(query,con);
    cmd.ExecuteNonQuery();
    con.Close();

 }

And here is second method using pool

string connectionString="Data Source = " + MyIp + "; Initial Catalog = xxxx; user id = xxxx; password = xxxx;Connection Timeout=3";

Btn.Click+=delegate
{
    string query="";
    query="Insert into Customers (name) values (dim)";

    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand(query,con))
    {
       connection.Open();
       cmd.ExecuteNonQuery();
    }
}

I need best performance for using SQL Server over the network. Time is important for me, so which of the two methods to use?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
daa daa
  • 143
  • 1
  • 2
  • 8

0 Answers0