return SQLite 值的最佳方式

Best way to return SQLite values

我在通用 Windows APP 中有一个 SQLite 数据库,并且有多种类型(bool、int 和 Uint64)。我需要 return 我 table 的所有值按 ID。

    // Id Module
    [PrimaryKey]
    public int id { set; get; }
    // Fix comm state
    public bool commState { set; get; }
    // Fix home sensor state
    public bool homeSensor { set; get; }
    // Fix up sensor state
    public bool upSensor { set; get; }
    // Fix down sensor state
    public bool downSensor { set; get; }
    // Fix actual tier position
    public int actualTier { set; get; }
    // Fix actual step of encoder
    public UInt64 encodPosition { set; get; }

我尝试通过列表 return 它,但我认为我不能 return 使用不同的类型。

return 从 SQLite 数据库中获取这些值的最佳方法是什么?

感谢您的帮助。

您可以使用 Entity Framework 来 return 获取您需要的对象。这是一篇带你一路走来的教程:

https://erazerbrecht.wordpress.com/2015/06/11/sqlite-entityframework-6-tutorial/

如果您想使用 ADO.NET,这里有一个方法可以 return 一个数据集(使用 Sqlite.Net):

    private static DataSet ExecuteDataset(string DBLocation, string query)
    {
        var conn = new SQLiteConnection("Data Source=" + DBLocation + ";Version=3;");
        DataSet ds;
        try
        {
            conn.Open();
            ds = new DataSet();
            var da = new SQLiteDataAdapter(query, conn);
            da.Fill(ds);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }

MethodMan帮助解决问题

我创建了一个对象:

class objModel
{
    // Id Module   
    public int id { set; get; }
    // Fix comm state
    public bool commState { set; get; }
    // Fix home sensor state
    public bool homeSensor { set; get; }
    // Fix up sensor state
    public bool upSensor { set; get; }
    // Fix down sensor state
    public bool downSensor { set; get; }
    // Fix actual tier position
    public int actualTier { set; get; }
    // Fix actual step of encoder
    public UInt64 encodPosition { set; get; }
}

然后使用对象列表:

public List<objModel> getParameters()
    {
        var db = new SQLiteConnection(new SQLitePlatformWinRT(), pathDBLocal);

        var sList = db.Table<ModuleDB>();

        List<objModel> mList = new List<objModel>();

        foreach (var item in sList)
        {
            mList.Add(item.objM);                   
        }
        return mList;
    }

谢谢大家...:)