I want to write a class which should handle my database connection.
I’m struggling to find a solution for my problem.
I have functions which are declaring the cmdSQLite
string and opening the connections. Especially for some use cases, where I have to manipulate the data at the time it’s being read (e.g. when using comboboxes, populating treeviews or having to convert only some of the data).
I would like to use a reader function like this: it should check the DbType
, then open the reader + read the data from the reader. At the end it should close the connection.
The commented part in the middle (marked with /* */) should be the code, which is different every time, as this code depends e.g. on the SQL string entered earlier.
public object getData()
{
switch(DbConn.DbType)
{
case 1:
using (SQLiteDataReader reader = cmdSQLite.ExecuteReader())
{
while (reader.Read())
{
/* //Inject custom code and RETURN DATA How?
cb_AG.Items.Add(reader.GetValue(2).ToString());
if (dtProj.Rows[0][3].ToString() == reader.GetValue(0).ToString())
{
cb_AG.SelectedIndex = i;
}
i++; */
}
}
connSQLite.Close();
return object; //Object has to be declared in given Textblock
case 2: //same, with MySQL
[...]
default:
return null;
}
}
Would be Object object = new Function();
a solution? But how to pass the function from the outside? Are there any pitfalls, which could be dangerous, when using that solution?
Or is there something better? I was also thinking about interfaces and delegates, but I’m not sure, if they will really fit into my class.
I’m stuck there as I don’t know, which ‘basic’ model I should use, which also won’t destroy the repairability of the code.
3