0

I am writing my first large application in C# and as it grows, I've started repeating the same code in every method that queries the database, more or less like this:

public static bool methodName(string ID, string Name, //etc)
{
    bool success = false;
    DBConnection db = new DBConnection(); //Oracle connection class
    db.Connect(); //Connect to the database

    if (db.GetConnectionState())  //checks the connection
    {
        db.SetSql("//insert, delete, update, etc, each method is different");
        db.addParameter("id", ID);

        if (db.ExecuteTransactions())  //attempts to insert, delete, update, whatever
            success = true;
        else
            success = false;
    }
    else
        success = false;

    db.Dispose();

    return success;    
}

I want only one method like this where the db.SetSql string can be anything. The problem is that every query needs different parameters passed to it from the user interface class, and that the number of db.addParameter calls will be different for a query to one table or another.

I know this is really basic, but I just can't get it. I really want these methods, which are multiplying exponentially every time I add a feature to the application, to be reduced down to one reusable one. Please help

0

2 Answers 2

2

You may use a Dictionary for your parameters;

public static bool methodName(string query, Dictionary<string, string> parameters)
{
    bool success = false;
    DBConnection db = new DBConnection(); //Oracle connection class
    db.Connect(); //Connect to the database

    if (db.GetConnectionState())  //checks the connection
    {
        db.SetSql(query);
        foreach(var param in parameters) db.addParameter(param.Key, param.Value);

        if (db.ExecuteTransactions())  //attempts to insert, delete, update, whatever
            success = true;
        else
            success = false;
    }
    else
        success = false;

    db.Dispose();

    return success;    
}

Now call it as follows:

methodName("Insert...", new Dictionary<string, string> {{"Id", ID }};
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of using KeyValuePair<string, string>[] he could pass a Dictionary.
This is great, but sometimes the values are int or DateTime, so Dictionary<string,string> wont work. Not sure if this is good practice (or if it'd even work) but could I do something like : ...methodName(string query, Dictionary<string, string> paramString, Dictionary<string, int> paramInt = 0,...)
Depending on what db.addParameter expects as datatype for the seond param, you may also use a Dictionary<string, object> where you can put anything into.
I just checked, it expects string, object, so I will do that
1

Consider using an ORM like NHibernate or Entity Framework. It does that stuff for you. Also look at coupling that with an inversion of control framework using something like autofac and you'll be much better off.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.