Results 1 to 3 of 3

Thread: looking for remarks on sqlserver code

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    looking for remarks on sqlserver code

    I haven't used SQLClient yet so this the first code I've written for it and I'm wondering if all the try catch block are REALLY needed and if the failed bool is a good idea. I plan to just throw the exception into an exception declared with the failed bool and use the last finally block to track the error in a log somewhere. This code is called in the Global.asax of a website so I don't want to display an error just track it.

    PHP Code:
    namespace PKPromo.Helpers.Data
    {
        
    /// <summary>
        /// DataHandler Class for the UsersOnline Session Information Table.
        /// </summary>
        
    public class UsersOnline
        
    {
            public static 
    void InitSession()
            {
                
    bool failed true;
                try
                {    
    //Attempt to create Session refreance and SqlClient Tools

                    //Where the hell on the website are we
                    
    string sessionID System.Web.HttpContext.Current.Session.SessionID;    //grab our SeesionID
                    
                    //Where does data come from mommy
                    
    string connStr SQLServer.ConnectionString;    //cache connection string
                    
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
                    
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Init"conn);

                    try
                    {    
    //Attempt access database and initialize UserSession
                        
    conn.Open();
                        
    cmd.CommandType System.Data.CommandType.StoredProcedure;
                        
    cmd.Parameters.Add("nSessionID",System.Data.SqlDbType.VarChar400);
                        
    cmd.Parameters["nSessionID"].Value sessionID;
                        
    int afct cmd.ExecuteNonQuery();
                        if(
    afct 0){failed false;}
                    }
                    catch(
    System.Exception ex)
                    {
                        
    failed true;
                        
    //track error later
                    
    }
                    finally
                    {
                        
    sessionID null;
                        
    cmd.Dispose();
                        
    conn.Dispose();
                    }
                }
                catch(
    System.Exception ex)
                {
                    
    failed true;
                    
    //track error later
                
    }
                finally
                {
                    if(
    failed)
                    {
                        
    //track error later
                    
    }
                }
            }

            public static 
    void DropSession()
            {
                
    bool failed true;
                try
                {    
    //Attempt to create Session refreance and SqlClient Tools

                    //Where the hell on the website are we
                    
    string sessionID System.Web.HttpContext.Current.Session.SessionID;    //grab our SeesionID
                    
                    //Where does data come from mommy
                    
    string connStr SQLServer.ConnectionString;    //cache connection string
                    
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
                    
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Finalize"conn);

                    try
                    {    
    //Attempt access database and initialize UserSession
                        
    conn.Open();
                        
    cmd.CommandType System.Data.CommandType.StoredProcedure;
                        
    cmd.Parameters.Add("nSessionID",System.Data.SqlDbType.VarChar400);
                        
    cmd.Parameters["nSessionID"].Value sessionID;
                        
    int afct cmd.ExecuteNonQuery();
                        if(
    afct 0){failed false;}
                    }
                    catch(
    System.Exception ex)
                    {
                        
    failed true;
                        
    //track error later
                    
    }
                    finally
                    {
                        
    sessionID null;
                        
    cmd.Dispose();
                        
    conn.Dispose();
                    }
                }
                catch(
    System.Exception ex)
                {
                    
    failed true;
                    
    //track error later
                
    }
                finally
                {
                    if(
    failed)
                    {
                        
    //track error later
                    
    }
                }
            }
        }

    Last edited by Magiaus; Apr 6th, 2004 at 01:32 PM.
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width