使用此代码:
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();
});
}
}
...我明白了,“ SQLite.SQLiteException未被用户代码处理 的HResult = -2146233088 Message =无法从未打开的数据库创建命令“
...但是尝试添加“db.Open()”不起作用,因为显然没有这样的方法。
答案 0 :(得分:7)
您过早地处理数据库(在事务内部)。最好在“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
});
});
}
}
答案 1 :(得分:1)
string connecString = @"Data Source=D:\SQLite.db;Pooling=true;FailIfMissing=false";
/*D:\sqlite.db就是sqlite数据库所在的目录,它的名字你可以随便改的*/
SQLiteConnection conn = new SQLiteConnection(connectString); //新建一个连接
conn.Open(); //打开连接
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from orders"; //数据库中要事先有个orders表
cmd.CommandType = CommandType.Text;
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
Console.WriteLine( reader[0].ToString());
}
您可以下载System.Data.SQLite.dll here
的中文文章