c# - LINQ-to-SQL .ExecuteCommand() doesn't work with parameterized object names -
c# - LINQ-to-SQL .ExecuteCommand() doesn't work with parameterized object names -
i'm little flummoxed 1 , hoping can clarify what's going on.
i'd programmatically disable foreign key constraint using linq-to-sql info context. seemingly should easy following:
context.executecommand( "alter table {0} nocheck constraint {1}", "mytable", "fk_mytable_myothertable" );
unfortunately, code bombs sql error "incorrect syntax near '@p0'."
when fire profiler see sql beingness generated is:
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 can find in sql books online , in linq-to-sql documentation should work.
i've resorted doing instead:
context.executecommand( string.format( "alter table {0} nocheck constraint {1}", "mytable", "fk_mytable_myothertable" ) );
it works, sort of defeats purpose of beingness able pass in parameters .executecommand().
so kind of quirk in linq-to-sql, sql server or confused?
any insights much appreciated!
as you've found out - linq-to-sql quite aggressively seek parametrize queries. good thing, in cases.
but in case of alter table...
statement, things table name etc. cannot parametrized - therefore, fails.
i don't know if there's way of getting linq-to-sql not parametrize queries executed executecommand
- meantime, think, statements don't involve manipulating info (inserting, updating etc.) rather manipulate construction of database, best bet utilize string.format(...)
approach actual sql query string, before calling .executecommand()
.
c# .net tsql sql-server-2008 linq-to-sql
Comments
Post a Comment