2017-05-30 108 views
1

有多個相關的問題,但他們都沒有解決我的問題。 我有一個連接到sqlite數據庫的C#(WPF)應用程序。有2個模型類 - 經銷商和訂單以及數據庫中的2個對應表。WPF SQLite查詢異常

我能夠做的CRUD操作中經銷商表,但試圖做同樣的訂單表時,我得到一個異常 - SQL邏輯錯誤或丟失的數據庫。我粘貼了一些相關的代碼:

對於經銷商

using(SQLiteConnection conn = new SQLiteConnection(connectionString)) { 
conn.Open(); 
using(SQLiteCommand cmd = new SQLiteCommand(conn)) { 
    cmd.CommandText = "INSERT INTO Dealer(name, address, contact, note) VALUES (@Name, @Address, @Contact, @Note)"; 
    cmd.Prepare(); 
    cmd.Parameters.AddWithValue("@Name", dealer.Name); 
    cmd.Parameters.AddWithValue("@Address", dealer.Address); 
    cmd.Parameters.AddWithValue("@Contact", dealer.Contact); 
    cmd.Parameters.AddWithValue("@Note", dealer.Note); 
    try { 
    result = cmd.ExecuteNonQuery(); 
    } 

對於訂購

using(SQLiteConnection conn = new SQLiteConnection(connectionString)) { 
conn.Open(); 
using(SQLiteCommand cmd = new SQLiteCommand(conn)) { 
    cmd.CommandText = "INSERT INTO Order(dealer_id, note) VALUES (@DealerId, @Note)"; 
    cmd.Prepare(); 
    cmd.Parameters.AddWithValue("@DealerId", order.DealerId); 
    cmd.Parameters.AddWithValue("@Note", order.Note); 
    try { 
    result = cmd.ExecuteNonQuery(); 
    } 

我得到的異常在cmd.ExecuteNonQuery();語句順序。

我已經交叉驗證了列名拼寫,並嘗試SELECT * FROM Order,它又返回了相同的異常。

我也試過SELECT name FROM sqlite_master WHERE type='table' AND name='Order';對於訂單上面的代碼來檢查表是否存在,它返回了正確的表名。

請幫忙。提前致謝。

回答

3

ORDER是SQLite的一個保留關鍵字,所以你應該使用[ ... ]引用它,比如:

cmd.CommandText = "INSERT INTO [Order] (dealer_id, note) VALUES (@DealerId, @Note)"; 

有SQLite中引用關鍵字的四種方法:https://sqlite.org/lang_keywords.html

+0

這工作。非常感謝.. – Abdullah