简体   繁体   English

LINQ-to-SQL.ExecuteCommand() 不适用于参数化的 object 名称

[英]LINQ-to-SQL .ExecuteCommand() doesn't work with parameterized object names

I'm a little flummoxed by this one and hoping that someone can clarify what's going on.我对此感到有些困惑,希望有人能澄清发生了什么。

I'd like to programmatically disable a foreign key constraint using my LINQ-to-SQL data context.我想使用我的 LINQ-to-SQL 数据上下文以编程方式禁用外键约束。 Seemingly this should be as easy as the following:看起来这应该像下面这样简单:

context.ExecuteCommand( "ALTER TABLE {0} NOCHECK CONSTRAINT {1}", "MyTable", "FK_MyTable_MyOtherTable" );

Unfortunately, this code bombs with the SQL error "Incorrect syntax near '@p0'."不幸的是,此代码会出现 SQL 错误“'@p0' 附近的语法不正确”。

When I fire up profiler I see that the SQL being generated is:当我启动分析器时,我看到正在生成的 SQL 是:

exec sp_executesql 
N'ALTER TABLE @p0 NOCHECK CONSTRAINT @p1',
N'@p0 varchar(4000),@p1 varchar(4000)',
@p0=N'MyTable',
@p1=N'FK_MyTable_MyOtherTable'

From everything I can find in SQL Books Online and in the LINQ-to-SQL documentation this should just work.从我在 SQL 在线图书和 LINQ-to-SQL 文档中可以找到的所有内容中,这应该可以正常工作。

I've resorted to doing this instead:我已经采取了这种做法:

context.ExecuteCommand( String.Format( "ALTER TABLE {0} NOCHECK CONSTRAINT {1}", "MyTable", "FK_MyTable_MyOtherTable" ) );

It works, but it sort of defeats the purpose of being able to pass in parameters to.ExecuteCommand().它可以工作,但它有点违背了能够将参数传递给.ExecuteCommand() 的目的。

So is this some kind of quirk in LINQ-to-SQL, SQL Server or am I just really confused?那么这是 LINQ-to-SQL、SQL 服务器中的某种怪癖还是我真的很困惑?

Any insights would be much appreciated!任何见解将不胜感激!

As you've found out - Linq-to-SQL will quite aggressively try to parametrize your queries.正如您所发现的 - Linq-to-SQL 将非常积极地尝试参数化您的查询。 Which is a good thing , in most cases.在大多数情况下,这是一件好事

But in the case of an ALTER TABLE... statement, things like the table name etc. cannot be parametrized - so therefore, it fails.但是在ALTER TABLE...语句的情况下,无法对表名等内容进行参数化 - 因此,它失败了。

I don't know if there's any way of getting Linq-to-SQL to not parametrize certain queries to be executed by ExecuteCommand - for the meantime, I think, statements that don't involve manipulating data (inserting, updating etc.) but rather manipulate the structure of your database, your best bet is to use the String.Format(...) approach to get the actual SQL query as a string, before calling .ExecuteCommand() .我不知道是否有任何方法可以让 Linq-to-SQL参数化由ExecuteCommand执行的某些查询 - 同时,我认为,语句不涉及操作数据(插入、更新等),而是操作结构在您的数据库中,最好的办法是在调用.ExecuteCommand()之前使用String.Format(...)方法将实际的 SQL 查询作为字符串获取。

You cant use SQL objects as parameters.您不能使用 SQL 对象作为参数。

So the second version is the only option.所以第二个版本是唯一的选择。

Linq to SQL is basically just translating what you have given it. Linq 到 SQL 基本上只是翻译你给它的东西。 The problem is that you can't have table names as parameters so the following does not work.问题是您不能将表名作为参数,因此以下内容不起作用。

DECLARE @TableName varchar

SET @Tablename='MyTable'

SELECT * FROM @TableName

It looks like you already have a solution, and I think it's a good one.看起来您已经有了解决方案,我认为这是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM