在 C# 中,我TRY..CATCH..FINALLY
在代码中使用了一个块来执行存储过程。
如果有异常,那么FINALLY
我想关闭阅读器 -PolicyResult
以及连接。
但是,我收到一个错误
当前上下文中不存在名称 PolicyResult
PolicyResult
是中定义的DataReader,但在该部分TRY
中似乎无法识别。FINALLY
为什么?
public static IEnumerable GetPolicies(int? MasterPkgID)
{
// Create a list of policies belonging to the master package.
List<AdditionalInterestPolicyData> additionalInterestPolicyData = new List<AdditionalInterestPolicyData>();
// Set the SQL connection to the database.
SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["QUESTIONNAIRE2"].ConnectionString);
try
{
// Open the connection.
objConn.Open();
// Get the list of policies by executing a stored procedure.
SqlCommand PolicyCmd = new SqlCommand("p_expapp_get_policy_detail_by_master_pkg", objConn);
PolicyCmd.Parameters.Clear();
PolicyCmd.CommandType = CommandType.StoredProcedure;
PolicyCmd.Parameters.Add("@a_master_package_iden_key", SqlDbType.Int).Value = MasterPkgID;
SqlDataReader PolicyResult = PolicyCmd.ExecuteReader();
// Loop thru the results returned.
while (PolicyResult.Read())
{
// Add to the list of policies - creates a new row for the collection.
additionalInterestPolicyData.Add(new AdditionalInterestPolicyData(
Int32.Parse(PolicyResult["affiliate_id"].ToString()),
Int32.Parse(PolicyResult["master_package_iden_key"].ToString())
)
);
}
}
catch (Exception ex)
{
bError = true;
}
finally
{
PolicyResult.Close();
objConn.Close();
}
return additionalInterestPolicyData;
}
}