WPF SQLite查询异常(WPF SQLite queries exception)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
WPF SQLite查询异常(WPF SQLite queries exception)

有许多相关的问题,但没有一个解决了我的问题。 我有一个连接到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

更多推荐

本文发布于:2023-04-24 21:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/83d1b1b8d7627f5f7568db700dd3e335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   SQLite   WPF   queries   exception

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!