Question

Basically I have a website that I am working on where there will be over 8 listboxes filled with information from databases. I currently use SqlDataSource because of ease of use and am using it currently databound to the listboxes.

Does SqlDataSource leave the connection open the whole time? I want to eliminate from an website architectural standpoint any unnecessary continuously open connections for security reasons as well as performance reasons.

Was it helpful?

Solution

Directly in answer to your question: No. The SqlDataSource control ensures that the connection is closed as soon as the operation it is required to perform has been completed.

OTHER TIPS

I used to use SQLDataAdapter + SQLCommand, but now I mostly use

using(SQLDataReader rdr = <YourSQLCommandVariable>.ExecuteReader())
{
    rdr.Load(<YourDataTableVariable))
}

Reason being I was unsure what data adapter did on top of the data reader to allow it to do batches of updates, reads and deletes. If you think about it, it would be extremely difficult to write a class like the data adapter that can do all that, without introducing any overhead. The overhead may not be significant, but unless I'm reading multiple tables out of a query into a DataSet object I don't run the risk of using it.

All that being said, I doubt any overhead on these operations is worth even considering if you locally cache all of the resulting data into the local machine. In other words, the biggest improvement you can make to your SQL queries is to not make them if the data is not likely to change over some time frame. If the data is updated once a day, cache it for 24 hours or less. Caching can be done either via the Session if it is end-user-dependent or via the HttpContext.Current.Cache object.

It sounds like you might want some tier separation in your application. The Web project is ideally ignorant of the database. Ideally there is some middle tier assembly that handles communicating with the database. Then from your .aspx.cs or Controller,depending on whether or not you're using MVC, you would make 8 calls to the middle tier (one for each listbox assuming they have distinct information). The middle tier would return something like List<MyObject> which you would then bind to the listbox.

My typical pattern for data access looks like this

using (SqlConnection conn = new SqlConnection("conn string"))
{
    conn.Open();
    SqlCommand command = new SqlCommand()
    {
        CommandText = "command text",
        Connection = conn,
        CommandType = CommandType.StoredProcedure //could be non-stored proc.. but would reccomend stored proc assuming SQL Server
    };

    command.Parameters.Add(new SqlParameter("MyParam", "param1"));
    command.Parameters.Add(new SqlParameter("MyParam2", "param2"));

    IDataReader reader = command.ExecuteReader();

    while(reader.Read())
    {
     //magic here
    }

    conn.Close();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top