我有如下 ADO.Net 代码,它调用存储过程。在存储过程中,我首先通过 SELECT 查询获取结果集,RAISERROR
如果传递的 @companyId 参数值不存在,则在 SELECT 语句调用后调用。我已经多次使用 @companyId 的值对此代码运行单元测试,因此RAISEERROR
被调用,但我从未看到ExecuteReader
引发错误的调用。为什么会发生这种奇怪的违反直觉的事情?
sqlCmd = new SqlCommand("dbo.xyz_sp_Attributes_GetValues", new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["VHA_EDM_ConnectionString"].ConnectionString));
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@attributeId", attributeId);
sqlCmd.Parameters.AddWithValue("@companyId", companyId);
sqlCmd.Parameters.AddWithValue("@attributeScope", "Company");
sqlCmd.Connection.Open();
SqlDataReader dr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
attributeValue = dr["AttributeValue"].ToString();
}
存储过程代码如下所示
SELECT col1, col2, ... where CompanyId = @companyId and AttributeId = @attributeId
if @companyId not exists (select companyId from Company where CompanyId = @companyId)
begin
set @errMessage = N'Invalid Company Error'
RAISERROR (@errMessage, 16, 1)
end