我有一种情况,我需要使用LINQ的ExecuteCommand方法来运行插入.
类似的东西(简化为这个问题的目的):
object[] oParams = { Guid.NewGuid(),rec.WebMethodID }; TransLogDataContext.ExecuteCommand ( "INSERT INTO dbo.Transaction_Log (ID,WebMethodID) VALUES ({0},{1})",oParams);
解决方法
做了一些研究,我发现这个:
In my simple testing,it looks like
the parameters passed in the
ExecuteQuery and ExecuteCommand
methods are automatically sql encoded
based on the value being supplied. So
if you pass in a string with a ‘
character,it will automatically sql
escape it to ”. I believe a similar
policy is used for other data types
like DateTimes,Decimals,etc.
http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx
(你有滚动的方式找到它)