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