if I want to select a record from table Person
then I would write query like this.
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db")))
{
connection.Query<Person>("SELECT * FROM Person Where Id=?",Id);
return true;
}
But what if I want to use the same four lines for multiple or dynamic tables? I want to pass dynamic table name instead of person. Like this.
void SelectDataForId(string tablename, int id)
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db")))
{
connection.Query<tablename>("SELECT * FROM "+tablename"+ Where Id=" + id);
return true;
}
}
I tried using Generic type of T as well but I am doing it wrong I guess.
void SelectDataForId<T>(T obj, string tablename, int id)
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db")))
{
var type = GetValue<T>(tablename);
connection.Query<type>("SELECT * FROM "+tablename"+ Where Id=" + id);
return true;
}
}
public static T GetValue<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
Please correct me where I am wrong.
Thanks