You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在C# ADO.NET中传递表值参数到存储过程时遇存储过程未找到错误

问题排查与解决

核心错误原因

你遇到的Could not find stored procedure ''错误,根源是代码里的存储过程名称末尾多了一个空格:

SqlCommand command = new SqlCommand("[External].[usp_SetSP] ", connection);

SQL Server会把带空格的字符串当作存储过程名去查找,但实际数据库里不存在这个带空格的存储过程,因此返回找不到的错误。

修正步骤

  1. 去掉存储过程名称末尾的空格,修正后的代码行:
SqlCommand command = new SqlCommand("[External].[usp_SetSP]", connection);
  1. 额外检查项(避免后续出现其他问题):
  • 确认[External].[usp_SetSP]存储过程确实存在于目标数据库,且当前连接的数据库用户拥有执行权限。
  • 检查表值参数的类型名[dbo].[EmpValuesList]是否正确,确保这个用户定义表类型已在数据库中创建。
  • 核对存储过程参数名:代码中用的@pEmpId、@pEmpList,要和存储过程定义的empid、emplist参数名对应(若存储过程参数带@前缀,代码里也需保持一致)。

修正后的完整代码

public async Task SetEmployeeDetails(string empId, DataTable empValues)
{
    using (var connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand("[External].[usp_SetSP]", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@pEmpId", empId);
        SqlParameter tvpParam = command.Parameters.AddWithValue("@pEmpList", empValues);
        tvpParam.SqlDbType = SqlDbType.Structured;
        tvpParam.TypeName = "[dbo].[EmpValuesList]";

        await connection.OpenAsync();
        await command.ExecuteNonQueryAsync();
    }
}

内容的提问来源于stack exchange,提问作者user21364415

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:33:22