fasa123 fasa123 - 1 year ago 36
SQL Question

c# undefined amount of object for data type (like params[])

Let's assume I have a sqlite database. That database has a table called main with 5 columns (id, c1, c2, c3, c4).

Client calls executeQuery which looks like this:

public Dictionary<int, Tuple<string, string, string, string>> executeSql(string statement) {
var ret = new Dictionary<int, Tuple<string, string, string, string>>;
var dbcmd = conn.CreateCommand();

for(int i = 0; i < Entries; i++) {
dbcmd.CommandText = statement.Replace("i", i.ToString());
var reader = dbcmd.ExecuteReader();
//.... Evalulate query....
ret.Add(); //add to Dic

I have no idea how many columns the client is asking for in his statement. Is there any type that allows me to work properly in such a scenario. What I want is sth like this:

Dictionary<int, Tuple<params[] string>>

which would provide me with a variable amount of strings to be added into the Tuple.

If there is nothing like that is there any clean way to solve this instead of using an array that will have empty entries (as it will break loops and is pretty terrible to read and mantain) nor do I want to have to use 90% of the same code just for every single case (aka executeSqlOne(), executeSqlTwo() etc...) as it would bloat the code heavily.


In that case, instead of Tuple<string, string, string, string> you can use List<string> I believe and you will not have to worry about no.of columns. Just add the resultant columns to the list and return the same.