PetaPoco 存储过程错误“关键字“FROM”附近的语法不正确。”}

发布于 2024-12-29 10:43:04 字数 797 浏览 5 评论 0原文

我将 C# 与 TSQL 和 SQL Server 2005 一起使用,

我尝试使用 PetaPoco 将数据集作为对象列表返回。这是我刚才使用的代码,

var s = PetaPoco.Sql.Builder.Append("USE [BI] EXEC [dbo].[TestProcedure2];");
            var result =  db.Query<dynamic>(s);

var result2 = db.Query<dynamic>("USE [BI] EXEC [dbo].[TestProcedure2];");

我认为错误消息是 petaPoco 失败时的通用 sql 错误。

起初,我使用带有参数的存储过程,@字符导致了问题,一旦用@@修复了这个问题,我就开始收到此错误,所以我用一个简单的 select 语句创建了一个存储过程。该过程在 Management Studio 中执行得非常好。

将 PetaPoco 与 select 语句一起使用就很好,并且数据可以完全映射到动态模型或对象模型。我创建了一个垃圾 SQL 字符串,它返回了相同的错误,这就是我从中得到通用错误想法的地方。

这是我正在使用的选择,效果很好

var dynTest =
                db.Query<dynamic>(
                   "SELECT TOP 10 * FROM [BI].[dbo].[Managers] ORDER  BY [ConsecutiveDays] desc");

I'm using C# with TSQL and SQL Server 2005

I'm trying to use PetaPoco to return a dataset as a list of objects. this is the code I'm using just now

var s = PetaPoco.Sql.Builder.Append("USE [BI] EXEC [dbo].[TestProcedure2];");
            var result =  db.Query<dynamic>(s);

var result2 = db.Query<dynamic>("USE [BI] EXEC [dbo].[TestProcedure2];");

I think the error message is a generic sql error for when petaPoco fails.

At first I was using a stored procedure with paramaters and the @ character was causing a problem, once that was fixed with @@ i started getting this error so I made a stored procedure with a simple select statement. The procedure executes completely fine in Management Studio.

Using PetaPoco with select statements is fine and the data is mapped both to a dynamic or an object model completely fine. I created a garbage SQL string and it returned the same error which is where I'm getting the generic error idea from.

This is the select I'm using which works fine

var dynTest =
                db.Query<dynamic>(
                   "SELECT TOP 10 * FROM [BI].[dbo].[Managers] ORDER  BY [ConsecutiveDays] desc");

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

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

发布评论

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

评论(2

深海夜未眠 2025-01-05 10:43:04

它试图在其前面附加 select 子句。
如果你加了一个“;”在查询开始时,它不会尝试附加它。

Its trying to append the select clause in front of it.
If you put a ";" at the start of your query it won't try to append it.

本宫微胖 2025-01-05 10:43:04

PetaPoco 假设您想要执行 SELECT,并且如果您不包含 SELECT,则会推断出一个。
为了避免执行自动 SELECT,您应该

db.EnableAutoSelect = false;

在查询之前使用:

PetaPoco assumes that you want to perform a SELECT and will infer one if you don't include one.
To avoid doing the automatic SELECT you should use:

db.EnableAutoSelect = false;

Prior to your query.

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