有许多相关的问题,但没有一个解决了我的问题。 我有一个连接到sqlite数据库的C#(WPF)应用程序。 数据库中有2个模型类 - 经销商和订单以及2个相应的表。
我能够在Dealer表中执行CRUD操作,但是当尝试在Order表中执行相同的操作时,我得到一个Exception - SQL Logic错误或缺少数据库 。 我粘贴了一些相关的代码:
经销商
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();获得异常cmd.ExecuteNonQuery(); 订单声明。
我已经交叉验证了列名拼写并尝试了SELECT * FROM Order ,它再次返回了相同的异常。
我也试过SELECT name FROM sqlite_master WHERE type='table' AND name='Order'; 从上面的For Order代码中检查表是否存在并返回正确的表名。
请帮忙。 提前致谢。
There are multiple related questions but none of them solved my problem. I have a C# (WPF) Application which connects to a sqlite database. There are 2 model classes - Dealer and Order and 2 corresponding tables in the Database.
I am able to do CRUD operations in Dealer table but when trying to do the same in Order table, I get an Exception - SQL Logic error or missing database. I am pasting some of the related code:
For Dealer
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(); }For Order
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(); }I get the exception at cmd.ExecuteNonQuery(); statement for Order.
I have cross verified column name spellings and tried SELECT * FROM Order which again returned the same exception.
I also tried SELECT name FROM sqlite_master WHERE type='table' AND name='Order'; from the For Order code above to check if table exists and it returned the correct table name.
Please help. Thanks in advance.
最满意答案
ORDER是SQLite中的保留关键字,因此您应引用它,例如使用[ ... ] :
cmd.CommandText = "INSERT INTO [Order] (dealer_id, note) VALUES (@DealerId, @Note)";在SQLite中有四种引用关键字的方法: https : //sqlite.org/lang_keywords.html
ORDER is a reserved keyword in SQLite so you should quote it, for example using [ ... ]:
cmd.CommandText = "INSERT INTO [Order] (dealer_id, note) VALUES (@DealerId, @Note)";There are four ways of quoting keywords in SQLite: https://sqlite.org/lang_keywords.html
更多推荐
发布评论