为什么当我添加参数时我的 ODBC SelectCommand 会被截断

发布于 2024-08-22 19:06:26 字数 744 浏览 2 评论 0原文

我正在使用 ODBC 连接来检索 C# 中的数据集。我可以看到在创建连接时完整的命令字符串已添加到连接中。

OdbcDataAdapter dataAdapter = new OdbcDataAdapter(GetCommandString(), odbcConnection);

我使用以下行将参数添加到命令字符串。

dataAdapter.SelectCommand.Parameters.Add("@Foo", OdbcType.Decimal).Value = foo2;

然而,当使用 SQL Profiler 时,我可以看到只有一部分命令字符串实际上进入了 SQL。

在测试过程中,我们发现,如果我们将值硬编码到字符串中并删除上面显示的参数行,则将传递完整字符串。

使用参数是否会限制我可以传递的字符串的长度?

编辑 - 添加有关 SQL 查询的信息:以下是添加参数之前查询的示例。

DECLARE @foo decimal
SET @foo = ?
Select c1,c2,c3,c4 from table1 where id = @foo
Select b1,b2,b3,b4 from table1 where id = @foo
Select a1,a2,a3,a4 from table1 where id = @foo
Select t1,t2,t3,t4 from table1 where id = @foo

I am using an ODBC connection to retrieve a data set in C#. I can see that the full command string is added to the connection when it is created.

OdbcDataAdapter dataAdapter = new OdbcDataAdapter(GetCommandString(), odbcConnection);

I am using the following line to add the parameter to the command string.

dataAdapter.SelectCommand.Parameters.Add("@Foo", OdbcType.Decimal).Value = foo2;

However, when using SQL Profiler I can see that only a portion of the command string actually makes it to SQL.

During testing we have seen that the full string will get passed in if we hard code a value into the string and remove the parameter line shown above.

Does using a parameter restrict the length of the string that I can pass though?

Edited - Adding info about SQL query: Here is an example of what the query looks like before the parameter is added.

DECLARE @foo decimal
SET @foo = ?
Select c1,c2,c3,c4 from table1 where id = @foo
Select b1,b2,b3,b4 from table1 where id = @foo
Select a1,a2,a3,a4 from table1 where id = @foo
Select t1,t2,t3,t4 from table1 where id = @foo

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

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

发布评论

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

评论(4

入怼 2024-08-29 19:06:26

“@Foo”是使用 OdbcCommand 的参数的正确语法吗? ODBC 处理参数的方式与 ADO.NET 不同。在 ODBC 中,参数按位置绑定,所有参数都使用占位符“?”在声明中。

我不确定 OdbcCommand 是否在命名参数和位置参数之间进行一些转换。也许确实如此,但未能成功重建命令。

另外,如果您仅连接到 SQL Server,请考虑仅使用 SqlClient。

Is "@Foo" the right syntax for parameters using the OdbcCommand? ODBC treats parameters differently than ADO.NET. In ODBC, parameters are bound by position and all parameters use the placeholder '?' in the statement.

I'm not sure if OdbcCommand does some translation between named and positional parameters. Maybe it does, and it's failing to rebuild the command successfully.

Also, if you are only connecting to SQL Server consider just using SqlClient.

许久 2024-08-29 19:06:26

您是否设置 scale< /a> 和精度?

截断是什么意思?您是否明白...

SELECT * FROM myTable WHERE decCol = 123

当您期望时

SELECT * FROM myTable WHERE decCol = 123.456

或者

SELECT * FROM myTable WHERE decCol = 123.456 AND OtherCol = 'bar'

此外,探查器将捕获整个查询文本。这让我想问,如果您可以使用 SQL Profiler,为什么还要使用 ODBC?

Are you setting scale and precision?

And what do you mean by truncate? Do you get this...

SELECT * FROM myTable WHERE decCol = 123

when you expect

SELECT * FROM myTable WHERE decCol = 123.456

or

SELECT * FROM myTable WHERE decCol = 123.456 AND OtherCol = 'bar'

Also, profiler will capture the whole query text. Which leads me to ask, if you can use SQL profiler why are you using ODBC?

怪我闹别瞎闹 2024-08-29 19:06:26

从您所显示的内容来看,您似乎走在正确的轨道上,但 "@Foo" 可能是错误的语法,但在没有看到 的实际输出的情况下我不能说GetCommandString()。 ODBC 语法为:

int foo2 = 5;
OdbcDataAdapter dataAdapter = new OdbcDataAdapter("SELECT * FROM Bar WHERE Foo = ?", odbcConnection);
dataAdapter.SelectCommand.Parameters.Add("Foo", OdbcType.Decimal).Value = foo2;

http://msdn.microsoft.com/en-us /library/zxdcah9t.aspx

From what you've shown it looks like you're on the right track, but "@Foo"is probably the wrong syntax, but I can't say without seeing the actual output from GetCommandString(). The ODBC syntax is:

int foo2 = 5;
OdbcDataAdapter dataAdapter = new OdbcDataAdapter("SELECT * FROM Bar WHERE Foo = ?", odbcConnection);
dataAdapter.SelectCommand.Parameters.Add("Foo", OdbcType.Decimal).Value = foo2;

http://msdn.microsoft.com/en-us/library/zxdcah9t.aspx

依 靠 2024-08-29 19:06:26

我同意其他人的观点,@Foo 语法可能不正确。如果您的 OdbcDriver 将 @Foo 理解为参数,它将不会注意它的名称,这可能会导致您以一种损坏的方式编写查询。假设这次您尝试重用一个参数(您的其他“工作”查询不这样做)满足此要求)

SELECT * FROM table WHERE col = @Foo 或 col2 = @Foo

在 Odbc 中将不起作用或默默地等效于:

SELECT * FROM table WHERE col = ?或 col2 = ?

您必须为两个参数添加参数值,您不能只添加一个名为“@Foo”的参数并让 Odbc 重用该值。此外,参数的名称并不重要,重要的是您将它们添加到右侧order:

cmd.Parameters.AddWithValue("col1Val", "foo")
cmd.Parameters.AddWithValue("column two value", "foo")

您为参数指定的名称只是为了方便您重用它们;司机不在乎他们叫什么

切换到?语法并查看您的问题是否仍然存在

I agree with the others, that the @Foo syntax may not be correct. If your OdbcDriver understands @Foo as a parameter it won't be heeding its name and this may be leading you to write queries in a broken way.. Suppose this time youre trying to reuse a parameter (your other "working" queries do not feature this requirement)

SELECT * FROM table WHERE col = @Foo or col2 = @Foo

In Odbc will either not work or silently become equivalent to:

SELECT * FROM table WHERE col = ? or col2 = ?

You'll have to add parameter values for both params, you cannot just add one parameter called "@Foo" and have Odbc reuse the value.. Further, it matters not what your parameters are called, only that you add them in the right order:

cmd.Parameters.AddWithValue("col1Val", "foo")
cmd.Parameters.AddWithValue("column two value", "foo")

The name you give to parameters is solely a convenience for your reuse of them; the driver cares not what they are called

Switch to ? syntax and see if your issue persists

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