c# - 使用 sqlite 从数据库中选择字符串值

标签 c# sqlite xamarin.android android-sqlite

我有一个问题。我正在尝试从我的数据库中获取一个值(字符串)。这是我调用以获取值的代码:

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);

我应该得到像 Nick 或 Steve 这样的值,但我得到这个值:

System.Collections.Generic.List`1[CryptoCurrencies.SettingDb]

我做错了什么?

最佳答案

您正在返回一个对象列表。

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;
    }
}

然后访问所需的属性。

关于c# - 使用 sqlite 从数据库中选择字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56549128/

相关文章:

c# - C#的System.data.sqlite教程

sql - 不使用限制的最高出现次数

android - MonoDroid 应用程序不使用正确的密度绘图

C# - Resources.GetDrawable 在 Android Xamarin 中已过时

C# 程序集重定向

c# - 在图像上画一个圆圈

sqlite - 如何反转SQLite CTE的顺序

c# - 选中/取消选中 TableLayout 中的多个复选框

c# - 使用 C# 从你在 TFS 2012 的团队项目中获取用户列表

c# - 为什么在这种情况下需要绑定(bind)