0

I'm sure this question has been asked many times, and I dug through a few of the similar ones but couldn't find one that really flushed it out as much as I'd have liked.

I have an application that uses a database helper class to connect and retrieve records from a database. I was considering rewriting it and wanted to know what the best way to do it would be.

Here is roughly how it's set up now (Note: This is already in place, and there are thousands of lines of this stuff).

DatabaseHelper.CS

    private SqlConnection conn; 

    public DatabaseHelper()
    {
        // Create database connection
        conn = new System.Data.SqlClient.SqlConnection();
        SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
        connection.ConnectTimeout = 150; // Microsft fix for timeout error (known bug)
        connection.MinPoolSize = 20; // Microsft fix for timeout error (known bug)
        connection.DataSource = Properties.Settings.Default.DBString;
        connection.InitialCatalog = Properties.Settings.Default.DBInitCatalog;
        connection.IntegratedSecurity = true;

        if (conn.State != ConnectionState.Connecting)
        {
            conn.ConnectionString = connection.ConnectionString;
        }
     }

    public bool Open()
    {
        if (this.IsOpen()) // IsOpen is just a method that checks connectionstate.
        { return true; }
        else
        {
            try
            {
                conn.Open();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                // omitted for post
            }
        }
        return false;
    }

    public bool Close()
    {
        if (!this.IsOpen())
        { return true; }
        try
        {
            conn.Close();
            return true;
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
           // omitted for post
        }
        return false;
    }

    public List<string> GetTeamLeaders(string team)
    {
        List<string> leaders = new List<string>();
        string query = "Select Leader FROM Teams WHERE Team = @team_vc";
        try
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@team_vc", SqlDbType.NVarChar).Value = team;
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    int column = sdr.GetOrdinal("Leader");
                    while (sdr.Read())
                    {
                        leaders.Add(sdr[column].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // omitted for post
        }
        return leaders;
    }

    private string GetTeamAbbrev(string team)
    {
        string abbrev= "";

        string query = "SELECT Abbrev FROM Teams where Team = @team_vc";
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            cmd.Parameters.Add("@team_vc", SqlDbType.NVarChar).Value = team;
            try
            {
                abbrev= Convert.ToString(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                // omitted for post
            }
        }
        return (string.IsNullOrEmpty(location)) ? "None" : abbrev;
    }

MainApp.CS

    private DatabaseHelper dbHelper;

    public MainApp()
    {
        InitializeComponent();
        dbHelper= new DatabaseHelper(); // Instantiate database controller
    }

    private void someButton_Click(object sender, EventArgs e)
    {
        List<string> teamLeaders = new List<string>();

        if (dbHelper.Open())
        {
            teamLeaders = dbConn.GetTeamLeaders(textboxTeam.Text);
            dbHelper.Close();
        }
        else
        {
            return;
        }
        // all the code to use results
    }

    private void someOtherButton_Click(object sender, EventArgs e)
    {
        List abbreviation = string.Empty;

        if (dbHelper.Open())
        {
            abbreviation = dbConn.GetTeamLeaders(textboxTeam.Text);
            dbHelper.Close();
        }
        else
        {
            return;
        }
        // all the code to use results
    }

Now I'm sure there are some very serious issues with how this is setup, but for me my biggest complaints are always having to open and close the connection.

My first move was to just move the open and close inside the DatabaseHelper methods, so each method (i.e. GetTeamLeaders) would call open and close in itself. But the problem was if it did actually fail to open it was really hard to feed it back up to the main program, which would try to run with whatever value the variable contained when it was made. I was thinking I would almost need an "out" bool that would tag along to see if the query completed, and could check make and check that anytime I used I needed to get something from the database, but I'm sure that has issues to.

Another big problem with this approach is anytime I want to make a call from another form, I have to either make another instance of the helper on that form, or pass a reference to the main one. (Currently my way around this is to retrieve all the information I would need beforehand in the MainApp and then just pass that to the new form). I'm not sure if when I rewrite this there's a good static way to set it up so that I can call it from anywhere.

So is there anything here worth keeping or does it all need to be stripped down and built back from scratch?

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
JWrightII
  • 942
  • 11
  • 26
  • 1
    You could make the `Datahelper` a singleton. But to be fair I think this is more a question for [codereview](http://codereview.stackexchange.com/) – Jens Kloster Feb 28 '13 at 19:49
  • Oh wow, I didn't even know that existed. I agree it would fit better there. Is there a correct way to move this over? Or should I just delete this one and post it there? – JWrightII Feb 28 '13 at 19:52
  • 1
    I dunno, but I think you should recreate it over there. I just discovered that place a week ago - so it still new to me :) – Jens Kloster Feb 28 '13 at 19:55

0 Answers0