我有一个函数,我原以为已经在代码分析中修复了CA2000警告,但是它就是不消失。警告出现在SqlCommand上。以下是该函数:
protected internal void LogUserSession(int? managerID)
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.SetCommand("usp_UserActivity_Create");
SqlParameter prmSessionID = new SqlParameter();
prmSessionID.ParameterName = "@sessionID";
prmSessionID.Direction = ParameterDirection.Input;
prmSessionID.SqlDbType = SqlDbType.VarChar;
prmSessionID.Size = 32;
prmSessionID.SetValue(SessionID);
SqlParameter prmUsername = new SqlParameter();
prmUsername.ParameterName = "@username";
prmUsername.Direction = ParameterDirection.Input;
prmUsername.SqlDbType = SqlDbType.VarChar;
prmUsername.Size = 32;
prmUsername.SetValue(Username);
SqlParameter prmLoginID = new SqlParameter();
prmLoginID.ParameterName = "@loginID";
prmLoginID.Direction = ParameterDirection.Output;
prmLoginID.SqlDbType = SqlDbType.Int;
sqlCommand.Parameters.Add(prmSessionID);
sqlCommand.Parameters.Add(prmUsername);
sqlCommand.Parameters.Add(prmLoginID);
using (sqlCommand.Connection = new SqlConnection(ConnectionStrings.MainApp))
{
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQueryTryCatch();
if (prmLoginID.Value != DBNull.Value) LoginID = Convert.ToInt32(prmLoginID.Value);
}
}
}
我还有一个函数,看起来和之前的函数没有什么不同,但是它没有与CA2000警告相关联。以下是该函数:
public static bool IsAvailable(string username)
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.SetCommand("usp_UsernameIsAvailable");
var prmUsername = new SqlParameter();
prmUsername.ParameterName = "@username";
prmUsername.Direction = ParameterDirection.Input;
prmUsername.SqlDbType = SqlDbType.VarChar;
prmUsername.Size = 32;
prmUsername.SetValue(username);
var prmReturnValue = new SqlParameter();
prmReturnValue.ParameterName = "@returnValue";
prmReturnValue.Direction = ParameterDirection.ReturnValue;
prmReturnValue.SqlDbType = SqlDbType.Bit;
sqlCommand.Parameters.Add(prmUsername);
sqlCommand.Parameters.Add(prmReturnValue);
using (sqlCommand.Connection = new SqlConnection(ConnectionStrings.ComplianceApps))
{
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQueryTryCatch();
return Convert.ToBoolean(prmReturnValue.Value);
}
}
}
我不明白这里发生了什么,也不知道需要做什么来解决它。