Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为Delete Top n命令修复@p0附近的SqlException错误语法_C#_Sql Server_Entity Framework 4 - Fatal编程技术网

C# 如何为Delete Top n命令修复@p0附近的SqlException错误语法

C# 如何为Delete Top n命令修复@p0附近的SqlException错误语法,c#,sql-server,entity-framework-4,C#,Sql Server,Entity Framework 4,我有以下语句在使用LINQ to Entity Framework 4的C程序中失败: int top = 1000; string name = "StagingTable"; using (var context = CreateObjectContext()) { int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP {0} FROM ", name), top); } 在程序的其他部分中正确创建了上

我有以下语句在使用LINQ to Entity Framework 4的C程序中失败:

int top = 1000;
string name = "StagingTable";

using (var context = CreateObjectContext())
{
    int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP {0} FROM ", name), top);
}
在程序的其他部分中正确创建了上下文,并且表名拼写正确。根据Microsoft文档,这可以从表中删除最大数量的记录,但会引发异常:

System.Data.SqlClient.SqlException:@p0附近的语法不正确

我反复检查了ExecuteStoreCommand的语法,没有发现任何错误


如何在这样的DELETE语句中使用TOP子句?

向TOP传递参数时,需要将其括在括号中:

int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP ({0}) FROM ", name), top);

将参数传递到TOP时,需要将其括在括号中:

int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP ({0}) FROM ", name), top);

当从Java执行SELECT TOP语句时,我在一篇类似但无关的帖子中找到了答案

如果将参数作为参数传递,SQL Server要求您在参数的周围加上括号

因此,有效的代码是:

int top = 1000;
string name = "StagingTable";
using (var context = CreateObjectContext())
{
    int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP ({0}) FROM ", name), top);
}

谢谢,.

当从Java执行SELECT TOP语句时,我在一篇类似但无关的帖子中找到了答案

如果将参数作为参数传递,SQL Server要求您在参数的周围加上括号

因此,有效的代码是:

int top = 1000;
string name = "StagingTable";
using (var context = CreateObjectContext())
{
    int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP ({0}) FROM ", name), top);
}
谢谢