locked
Enterprise Library Database connection close RRS feed

  • Question

  • We are using Microsoft Enterprise library to access the sql server database.We are having some doubts about how to close the DB Connection.Code is given below.

    DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(), false);
        Database db = new DatabaseProviderFactory().Create("Dataconnectionstring");
        string sqlCommand = "";
        DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
        dbCommand.CommandTimeout = 0;
        try
        {
          success = Convert.ToInt32(db.ExecuteScalar(dbCommand));
          return success;
        }

    catch (Exception e)
                {
                    throw e;
                }
                  so how  can we close database connection.    



    • Edited by tssubin Friday, June 20, 2014 5:08 PM
    • Moved by Charles-Li Monday, June 23, 2014 1:52 AM
    Friday, June 20, 2014 5:03 PM

Answers

  • Hi tssubin,

    Please read this MSDN document,

    http://msdn.microsoft.com/en-us/library/ff648933.aspx

    Database connections are a limited resource, and proper management of them is essential for scalable applications. It is good practice to keep connections open only as long as they are needed and to close them as soon as practical. By design, most of the Database class methods handle the opening and closing of connections to the database on each call. Therefore, the application code does not need to include code for managing connections. (By default, and for performance reasons, ADO.NET returns connections to the connection pool without closing them. Therefore, you do not need to cache your Database objects.)

    So you don't need to close the connection manually, the ExecuateScalar method will close it automatically.

    If you have further questions about Enterprise Library, I recommend you post your questions in this online community for help:

    http://entlib.codeplex.com/

    Thank you!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Caillen Monday, June 23, 2014 1:58 AM
    • Marked as answer by Caillen Friday, June 27, 2014 10:58 AM
    Monday, June 23, 2014 1:56 AM

All replies

  • Hi tssubin,

    Since this thread is more related to C#, I will move it to the right forum for a better response. Thanks for your understanding.

    Best regards, 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 23, 2014 1:51 AM
  • Hi tssubin,

    Please read this MSDN document,

    http://msdn.microsoft.com/en-us/library/ff648933.aspx

    Database connections are a limited resource, and proper management of them is essential for scalable applications. It is good practice to keep connections open only as long as they are needed and to close them as soon as practical. By design, most of the Database class methods handle the opening and closing of connections to the database on each call. Therefore, the application code does not need to include code for managing connections. (By default, and for performance reasons, ADO.NET returns connections to the connection pool without closing them. Therefore, you do not need to cache your Database objects.)

    So you don't need to close the connection manually, the ExecuateScalar method will close it automatically.

    If you have further questions about Enterprise Library, I recommend you post your questions in this online community for help:

    http://entlib.codeplex.com/

    Thank you!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Caillen Monday, June 23, 2014 1:58 AM
    • Marked as answer by Caillen Friday, June 27, 2014 10:58 AM
    Monday, June 23, 2014 1:56 AM