Handle database connections with care

Rate It (1)

Last post 03-22-2009 4:47 PM by yorock. 36 replies.

Sort Posts:

  • Re: Handle database connections with care

    10-26-2008, 12:19 AM
    Locked
    • Member
      25 point Member
    • SirPaladin1969
    • Member since 02-14-2008, 10:48 PM
    • Albany, NY
    • Posts 58

    I never got an anwer on this: Whose UserName and Passwords would be hard coded in the web.config? Wouldn't each user need to login with their own UserName and Password to gain access? Do you add Keys for each? That would seem very inefficient if you had lots of users.

  • Re: Handle database connections with care

    11-20-2008, 10:38 AM
    Locked
    • Member
      2 point Member
    • sriyasarkar
    • Member since 11-20-2008, 3:21 PM
    • Iowa, USA
    • Posts 2

    I have one question.

    If I am using a datareader, should I close that in the try block or the finally block?

  • Re: Handle database connections with care

    11-25-2008, 12:46 PM
    Locked
    • Participant
      1,217 point Participant
    • vijjendra
    • Member since 10-13-2008, 8:17 AM
    • Noida
    • Posts 263

    when you use datareader first declare as null

    dataReader dr=null;

    and use using because using is the combination of try and finally.

     

     
    Code Review Service || Join now: CodeASP.NET Community

    Please mark the most helpful reply/replies as “Answer”.
  • Re: Handle database connections with care

    11-29-2008, 6:27 AM
    Locked
    • Member
      6 point Member
    • abc@abc.com
    • Member since 11-29-2008, 11:19 AM
    • Posts 3

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// <summary>
    /// Summary description for Cls_DataAccess
    /// </summary>
        public class Cls_DataAccess
        {
            private SqlConnection con;
            public bool blnIsError;
            public string strErrorMessage;
            public Cls_DataAccess()
            {
                //
                // TODO: Add constructor logic here
                //
                blnIsError = false;
                strErrorMessage = "";
            }

            /// <summary>
            /// Returns Error Message
            /// </summary>
            /// <param name="ResetError">Boolean value to specify if error needs to be reset</param>
            /// <returns></returns>
            public string GetErrorMessage(bool ResetErrorStatus)
            {
                string strReturn = strErrorMessage;

                if (ResetErrorStatus == true)
                {
                    ResetError();
                }

                return strReturn;
            }

            /// <summary>
            /// Initilaizes the error message of the class
            /// </summary>
            /// <param name="ErrorMessage"></param>


            private void InitializeError(string ErrorMessage, string Function)
            {
                blnIsError = true;
                strErrorMessage = ErrorCodes.ReturnError(this.ToString(), Function, ErrorMessage);
            }

            /// <summary>
            /// Resets the error
            /// </summary>
            public void ResetError()
            {
                blnIsError = false;
                strErrorMessage = "";
            }

            /// <summary>
            /// Returns an open database connection
            /// </summary>
            /// <returns>Connection Object</returns>
            public SqlConnection GetConnection()
            {
               

                //olecon.ConnectionString = ConfigurationManager.ConnectionStrings["tempStoreConnString"].ConnectionString;

                string strConnectionString = ConfigurationManager.ConnectionStrings["tempConnString"].ConnectionString;

                strConnectionString = strConnectionString.Replace("App_Data/", @""+AppDomain.CurrentDomain.BaseDirectory+@"App_Data/");

                con = new SqlConnection();
               
                con.ConnectionString = strConnectionString;
               
                try
                {
                    if (olecon.State != System.Data.ConnectionState.Open)
                    {
                        con.Open();
                    }
                    return con;
                }
                catch (Exception errOlecon)
                {
                    InitializeError(errOlecon.Message, "GetConnection");
                    return null;
                }
            }

            /// <summary>
            /// Close the database connection if opened
            /// </summary>
            public void CloseConnection()
            {
                if (con.State == System.Data.ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }

    Filed under:
  • Re: Handle database connections with care

    12-31-2008, 5:03 AM
    Locked
    • Contributor
      7,269 point Contributor
    • sirdneo
    • Member since 12-16-2008, 5:45 AM
    • Karachi, Pakistan
    • Posts 1,146

    Keeping connection open for bunch of operation or open/close connection for every operation depends on application behavior and usage.

     

    I always use connection pool  which have multiple connections and always colse connection for each operation. But sometimes I use same connection from pool for multple operation.

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
  • Re: Handle database connections with care

    03-09-2009, 5:24 AM
    Locked
    • Contributor
      2,217 point Contributor
    • sudipta
    • Member since 02-25-2008, 3:33 PM
    • India
    • Posts 452

    Hi Sriya,

     Replying to a very old post :)

    Use the code as below in your try block.

    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    
    if (rdr.Read())
    {
       str = rdr[0].ToString();
    }
    
    rdr.Close();
     
    Please click "Mark As Answer" if this hepled in solving your problem.
  • Re: Handle database connections with care

    03-22-2009, 4:47 PM
    Locked
    • Member
      2 point Member
    • yorock
    • Member since 02-14-2009, 2:44 PM
    • Posts 3

    hi all,

    whenever i try 2 execute the connection code.A server error is gettin displayed "cannot open the database "projdb.mdf"requested .login failed..login failed for the user "YOGESH\Owner"

Page 3 of 3 (37 items) < Previous 1 2 3
Microsoft Communities