为什么我的SQL CLR存储过程调用声称我没有提供参数?

时间:2014-04-09 17:53:06

标签: c# sqlclr

在CLR程序中,我有以下方法:

private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string storedProcedureName = @"up_RemoveAssetOwnershipFromTeam";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(storedProcedureName, connection))
    {
        command.Parameters.AddWithValue("assetId", assetId);
        command.Parameters.AddWithValue("teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

当我运行此方法时,我收到以下错误:

  

Msg 6522,Level 16,State 1,Procedure cp_RemoveAsset,Line 0

     

在执行用户定义的例程或聚合期间发生了.NET Framework错误" cp_RemoveAsset":

     

System.Data.SqlClient.SqlException:过程或函数' up_RemoveAssetOwnershipFromTeam'期望参数' @ assetId',这是未提供的。

     

System.Data.SqlClient.SqlException:

     

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection)

     

at System.Data.SqlClient.SqlCommand.RunExecuteNonQuerySmi(Boolean sendToPipe)

     

at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result,String methodName,Boolean sendToPipe)

     

在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

     

at StoredProcedures.EndOwnershipForTeam(Int64 assetId,Int32 teamId)

     

at StoredProcedures.cp_RemoveAsset(SqlInt32 userId,SqlString xaid)

由于我的代码提供了参数(通过SqlContext.Pipe.Send()调用显示输出进行验证),为什么它声称我没有提供实际参数?

3 个答案:

答案 0 :(得分:3)

您遗漏了行中的@符号

command.Parameters.AddWithValue("assetId", assetId);
command.Parameters.AddWithValue("teamId", teamId);

他们应该

command.Parameters.AddWithValue("@assetId", assetId);
command.Parameters.AddWithValue("@teamId", teamId);

答案 1 :(得分:3)

看起来像写的,你的代码指示SQL Server只是尝试执行这个:

up_RemoveAssetOwnershipFromTeam

换句话说,只是没有提供参数的程序。

要连接所需的参数,要么指定CommandType.StoredProcedure,要么明确地连接命令参数:

// option 1
private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string storedProcedureName = @"up_RemoveAssetOwnershipFromTeam";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(storedProcedureName, connection))
    {
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("assetId", assetId);
        command.Parameters.AddWithValue("teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

// option 2
private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string sql = @"exec up_RemoveAssetOwnershipFromTeam @assetId, @teamId";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@assetId", assetId);
        command.Parameters.AddWithValue("@teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

答案 2 :(得分:0)

  1. 您省略了'@'前缀。
  2. 为sql server和/或数据库配置的默认排序规则是什么?如果区分大小写,则需要完全匹配大小写。