0

This is a function which I used to get data from database

 public static DataTable getDataTable(string sql, string tableName)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(strConn));
            da.Fill(dt);
            dt.TableName = tableName;
        }
        catch (Exception)
        {
            dt = null;
        }
        return dt;
    }

The questions are:

  1. Does it open and close connection automatically? because it seems we only pass the sql query to SqlDataAdapter and it has no open or close the connection.
  2. Does it will cause any performance to slow down the application?
  3. Does it will cause any performance issue the server (Memory)?

Thanks in advance.

CeebLaj Thoj
  • 81
  • 2
  • 10

1 Answers1

0

i hope this help you

Answer 1 : If I remember correctly the SQLConnection manages connection pooling behind the scenes.

Here is a nice article about this very subject: https://msdn.microsoft.com/en-us/library/ms971481.aspx

Also, a best practice is to use the "using" keyword to wrap your connection because it will automatically close your connection once the scope exits (with or without an exception)

  using(SqlConnection conn = new SqlConnection())
  {
    conn.Open();
    //Do Work with connection

  } //Connection automatically closes here without the need to call conn.Close()

Answer 2: Sql Connection open slow down or make application performance better

Answer 3 : What is Best Approach for Opening/Closing SqlConnection in C#

Ali Tooshmalani
  • 241
  • 2
  • 7