locked
DataBase connection Recommended RRS feed

  • Question

  • User418394696 posted

     Hi all!!

    I've got an ASP.NET web project which connect to an SQL Server 2005 DataBase.

    I think that I'm doing the part of the database connection awfull because everytime I need to do a query (select for example) I'm creating a new connection, opening the connection, creating the command, executing the query, using the results and closing the connection.

    Is the rigth way to open a connection in the start of the application and closing at the end??

    How can I do that??

    I'm retrieving data but what will happen to database server when the application will be used by many people???

    Any help or link to do that??

     

    Thank You!!!

    Monday, July 5, 2010 5:56 PM

Answers

  • User-311685349 posted

    It's not the most efficient way to do things (in terms of having to code all that up), there are frameworks like EntityFramework, nHibernate, LINQ to SQL which generate a lot of this stuff for you.

    .NET manages the database connections (assuming you're using the identical string, which is case sensitive).

    It's good practice to keep you're connection object within a try...finally block (or using(...) block)

    It's best also to open the connection for each call to a page and close it immediately after you've finished with it.

    Hope this helps

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 5, 2010 8:42 PM

All replies

  • User-311685349 posted

    It's not the most efficient way to do things (in terms of having to code all that up), there are frameworks like EntityFramework, nHibernate, LINQ to SQL which generate a lot of this stuff for you.

    .NET manages the database connections (assuming you're using the identical string, which is case sensitive).

    It's good practice to keep you're connection object within a try...finally block (or using(...) block)

    It's best also to open the connection for each call to a page and close it immediately after you've finished with it.

    Hope this helps

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 5, 2010 8:42 PM
  • User418394696 posted

    I'll search for more information about that things.

    thanks!

    Tuesday, July 6, 2010 1:47 PM