2012-12-20 87 views
3

使用此代码:无法打开SQLite数据库

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 消息=不能创建从未拆封数据库命令”

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

+0

确定吗?本网站http://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteConnection.html不同意。 – PhoenixReborn

+0

我没有使用dotconnect;也许他们有一个Open方法。 –

回答

6

你过早地配置数据库(内部交易)一个中国的文章。最好在“使用”语句中包装一下,它将处理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
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

这里是csharp connect sqlite