无法打开SQLite数据库

B. *_*non 4 c# sqlite windows-8 windows-store-apps sqlite-net

使用此代码:

public void InsertPlatypiRequestedRecord(string PlatypusId, string PlatypusName, DateTime invitationSentLocal)
{
    var db = new SQLiteConnection(SQLitePath);
    {
        db.CreateTable<PlatypiRequested>();
        db.RunInTransaction(() =>
            {
                db.Insert(new PlatypiRequested
                              {
                                  PlatypusId = PlatypusId,
                                  PlatypusName = PlatypusName,
                                  InvitationSentLocal = invitationSentLocal
                              });
                db.Dispose();
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

...我得到," SQLite.SQLiteException未被用户代码处理HResult = -2146233088 Message =无法从未打开的数据库创建命令 "

...但是尝试添加"db.Open()"不起作用,因为显然没有这样的方法.

chu*_*e x 8

您过早地处理数据库(在事务内部).最好将内容包装在"using"语句中,该语句将处理db连接:

private void InsertPlatypiRequestedRecord(string platypusId, string platypusName, DateTime invitationSentLocal)
{
    using (var db = new SQLiteConnection(SQLitePath))
    {
        db.CreateTable<PlatypiRequested>();
        db.RunInTransaction(() =>
        {
            db.Insert(new PlatypiRequested
            {
                PlatypusId = platypusId,
                PlatypusName = platypusName,
                InvitationSentLocal = invitationSentLocal
            });
        });
    }
}
Run Code Online (Sandbox Code Playgroud)