C# 无法打开SQLite数据库
使用此代码:C# 无法打开SQLite数据库,c#,sqlite,windows-8,windows-store-apps,sqlite-net,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
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();
});
}
}
public void insertPlatypRequestedRecord(字符串PlatypusId、字符串PlatypusName、日期时间邀请EntLocal)
{
var db=新的SQLiteConnection(SQLitePath);
{
db.CreateTable();
db.RunInTransaction(()=>
{
db.Insert(需要新平台)
{
鸭嘴兽,
鸭嘴兽名称=鸭嘴兽名称,
InvitationSentLocal=InvitationSentLocal
});
db.Dispose();
});
}
}
…我得到,“SQLite.SQLiteException未被用户代码处理
HResult=-2146233088
Message=无法从未打开的数据库创建命令“
…但尝试添加“db.Open()”无效,因为显然没有这样的方法
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
这是一篇中文文章,内容是关于您过早地(在事务内部)处理数据库的。最好将内容封装在“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
});
});
}
}
private void insertPlatypRequestedRecord(字符串platypusId、字符串platypusName、日期时间邀请EntLocal)
{
使用(var db=new SQLiteConnection(SQLitePath))
{
db.CreateTable();
db.RunInTransaction(()=>
{
db.Insert(需要新平台)
{
鸭嘴兽,
鸭嘴兽名称=鸭嘴兽名称,
InvitationSentLocal=InvitationSentLocal
});
});
}
}
你确定吗?这个网站,不同意。我没有使用dotconnect;也许他们确实有一个开放的方法。