使用 SQLite 的 LINQ-to-SQL:“SELECT”附近的语法错误插入时

发布于 2024-09-03 20:48:39 字数 1037 浏览 2 评论 0 原文

我一直在为我的应用程序使用 System.Data.SQLite 提供程序。然而,当我尝试创建新对象并将其插入数据库时​​,我在“SELECT”附近收到 SQL 语法错误

基本上我的代码看起来像:

//supplies is a DataContext connected to my DB
Table<Store> stores = supplies.Stores;
//...
Store newStore = new Store();
// A Store has an Id (pk autoincrement), Name, Number, and EntitySet<Usages>
newStore.Name = "New Store";
newStore.Number = 0;
//...
stores.InsertOnSubmit(newStore);
supplies.SubmitChanges();

但分布在一些方法中。

这是一个常见/新手问题吗?如果是这样,我做错了什么? 如果没有,我应该在哪里/如何调试它?我对 LINQ-to-SQL 相当陌生。

UPDATE:错误发生前生成的最后一个查询的日志输出是这样的:

INSERT INTO [Stores]([Number], [Name])
VALUES (@p0, @p1)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [0]
-- @p1: Input String (Size = 0; Prec = 0; Scale = 0) [New Store]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

I have been using the System.Data.SQLite provider for my application. When I try to create a new object and insert it into the database, however, I get a SQL syntax error near "SELECT".

Basically my code looks like:

//supplies is a DataContext connected to my DB
Table<Store> stores = supplies.Stores;
//...
Store newStore = new Store();
// A Store has an Id (pk autoincrement), Name, Number, and EntitySet<Usages>
newStore.Name = "New Store";
newStore.Number = 0;
//...
stores.InsertOnSubmit(newStore);
supplies.SubmitChanges();

but spread throughout some methods.

Is this a common/newbie problem? If so, what am I doing wrong?
If not, where/how should I debug this? I am rather new at LINQ-to-SQL.

UPDATE: The log output of the last query generated before the error is this:

INSERT INTO [Stores]([Number], [Name])
VALUES (@p0, @p1)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [0]
-- @p1: Input String (Size = 0; Prec = 0; Scale = 0) [New Store]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-09-10 20:48:39

这里有一篇关于如何显示正在生成的 SQL 的文章,这可能会更好地了解 SQLite 不喜欢的内容:http://msdn.microsoft.com/en-us/library/bb386961.aspx

编辑日志:从 SqlProvider(Sql2008) 部分来看,它似乎正在尝试使用SqlServer 2008 语法而不是 SQLite 语法,这可以解释为什么它认为该语法无效。

再次编辑以获取更多信息:有关 SQLite 和 Linq 的信息,请参阅此问题:LINQ with SQLite (linqtosql)

There's an article here about how to show the SQL being generated which might give a better idea of what SQLite doesn't like: http://msdn.microsoft.com/en-us/library/bb386961.aspx

EDIT for log: From the SqlProvider(Sql2008) part, it looks like it's trying to use SqlServer 2008 syntax instead of SQLite syntax which would explain why it thinks the syntax is invalid.

EDIT again for further info: See this question for info on SQLite and Linq: LINQ with SQLite (linqtosql)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文