C#代码分析CA2000 [英] C# Code Analysis CA2000

查看:316
本文介绍了C#代码分析CA2000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我以为我已经固定的代码分析为CA2000警告功能,但它只是不会消失。该警告是对的SqlCommand。这里的功能:

I have a function which I thought I had fixed the CA2000 warning in Code Analysis for, but it just won't go away. The warning is on SqlCommand. Here's the function:

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警告。下面是函数:

I have another function that to me looks no different but does not have a CA2000 warning associated to it. Here's that function:

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);
                }
            }
        }



我不明白什么是怎么回事,我需要做什么来解决它的。

I don't understand what's going on here and what I need to do to fix it.

推荐答案

的CA2000警告是臭名昭著的引起误报。其中的一个做的事情是,当它发现可以抛出一个异常,超过16个可能的位置,它只是停止寻找和标志的CA警告。

The CA2000 warning is notorious for causing false positives. One of the things it does is when it finds more than 16 possible locations that can throw an exception, it just stops looking and flags the CA warning.

有一个非常类似的问题与微软的回应可以在这里找到:
http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/90f993a3-6bdf-4b62-9982-9247a655406d/

A very similar question with a response from Microsoft can be found here: http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/90f993a3-6bdf-4b62-9982-9247a655406d/

连接bug跟踪此问题:
https://connect.microsoft.com/VisualStudio/feedback/details/725836/warning-ca2000-is-fired-on-a-的SqlCommand与 - 很多-sqlparameters#详细信息

Connect bug tracking this issue: https://connect.microsoft.com/VisualStudio/feedback/details/725836/warning-ca2000-is-fired-on-a-sqlcommand-with-many-sqlparameters#details

这篇关于C#代码分析CA2000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆