2012-12-20 86 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