我有问题。我正在尝试从数据库中获取一个值(字符串)。这是我要获取值的代码:
public string SelectValueFromTableSettings(string name)
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
{
return connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).ToString();
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
下面是将值分配给变量的代码:
string SwitchValue = MainActivity.db.SelectValueFromTableSettings(mItems[position].Name);
我应该得到一个像尼克或史蒂夫这样的价值,但我会把它作为价值得到
System.Collections.Generic.List`1 [CryptoCurrencies.SettingDb]
我在做什么错了?
答案 0 :(得分:3)
您要返回对象列表。
connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).FirstOrDefault().YourProperty.ToString();
或者,只需返回对象:
public SettingDB SelectValueFromTableSettings(string name)
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
{
return connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).FirstOrDefault();
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
然后访问所需的属性。