在C# ADO.NET中传递表值参数到存储过程时遇存储过程未找到错误
问题排查与解决
核心错误原因
你遇到的Could not find stored procedure ''错误,根源是代码里的存储过程名称末尾多了一个空格:
SqlCommand command = new SqlCommand("[External].[usp_SetSP] ", connection);
SQL Server会把带空格的字符串当作存储过程名去查找,但实际数据库里不存在这个带空格的存储过程,因此返回找不到的错误。
修正步骤
- 去掉存储过程名称末尾的空格,修正后的代码行:
SqlCommand command = new SqlCommand("[External].[usp_SetSP]", connection);
- 额外检查项(避免后续出现其他问题):
- 确认
[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
相关产品推荐
相关产品推荐

