在VS2010中对项目运行代码分析

时间:2012-12-19 16:36:39

标签: c# visual-studio-2010 code-analysis

我在我的项目上运行代码Analysis。我收到8个警告和0个错误。我不确定它们是什么意思,但我有6个相同的代码(CA2000),另外2个是相同的代码(CA2240)。这是一个常见的警告吗?

Warning 1 CA2000 : Microsoft.Reliability : In method 'AdminDisplay.AdminDropDown_SelectedIndexChanged(object, EventArgs)', call System.IDisposable.Dispose on object 'ad' before all references to it are out of scope. 

Warning 2 CA2000 : Microsoft.Reliability : In method 'AdminDisplay.AdminDropDown_SelectedIndexChanged(object, EventArgs)', call System.IDisposable.Dispose on object 'cmd' before all references to it are out of scope. 

Warning 3 CA2000 : Microsoft.Reliability : In method 'AdminDisplay.AdminDropDown_SelectedIndexChanged(object, EventArgs)', call System.IDisposable.Dispose on object 'conn' before all references to it are out of scope. 

Warning 5 CA2240 : Microsoft.Usage : Add an implementation of GetObjectData to type 'FoundationDataSet'.

Warning 7 CA2000 : Microsoft.Reliability : In method 'WebForm1.ExecuteInsert(string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string)', object 'conn' is not disposed along all exception paths.

我有“使用”但我仍然收到警告。我的语法不正确吗?

using (SqlConnection conn = new SqlConnection("Data Source=xx.xx.x.xx;Initial Catalog=tablenamae;User ID=xxx;Password=xxxxxxx"))
{
    DataTable dt = new DataTable();

    conn.Open();

    SqlCommand cmd = new SqlCommand("GetStudentInfo", conn);

    cmd.CommandType =CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@ID", AdminDropDown.SelectedValue));

    //cmd.Connection.Open();
    //cmd.ExecuteNonQuery();

    SqlDataAdapter ad = new SqlDataAdapter(cmd);

    ad.Fill(dt);     

    if (dt.Rows.Count > 0)
    {
        //If you want to get mutiple data from the database then you need to write a simple looping
        txtFirstName.Text = dt.Rows[0]["FirstName"].ToString();

        txtMiddleName.Text = dt.Rows[0]["MiddleName"].ToString();

        txtLastName.Text = dt.Rows[0]["LastName"].ToString();

        txtSignature.Text = dt.Rows[0]["Signature"].ToString();

    }

    cmd.Connection.Close();
}  

有关如何解决这些错误的任何想法?感谢

1 个答案:

答案 0 :(得分:1)

只需添加例如ad.Dispose()代码。警告不是关于语法,而是关于missing call to Dispoable objects(这可能导致内存泄漏和其他问题)。如果对象实现IDisposable,您可以将其放入使用块中。垃圾收集的关系是demystified here

using (SqlConnection conn = new SqlConnection("Data Source=xx.xx.x.xx;Initial Catalog=tablenamae;User ID=xxx;Password=xxxxxxx"))
{
    DataTable dt = new DataTable();

    conn.Open();

    SqlCommand cmd = new SqlCommand("GetStudentInfo", conn);

    cmd.CommandType =CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@ID", AdminDropDown.SelectedValue));

    //cmd.Connection.Open();
    //cmd.ExecuteNonQuery();

    SqlDataAdapter ad = new SqlDataAdapter(cmd);

    ad.Fill(dt);     

    if (dt.Rows.Count > 0)
    {
        //If you want to get mutiple data from the database then you need to write a simple looping
        txtFirstName.Text = dt.Rows[0]["FirstName"].ToString();

        txtMiddleName.Text = dt.Rows[0]["MiddleName"].ToString();

        txtLastName.Text = dt.Rows[0]["LastName"].ToString();

        txtSignature.Text = dt.Rows[0]["Signature"].ToString();

    }

    ad.Dispose();  // e.g. this way

    cmd.Connection.Close();
}

...或者更好(就像你已经使用SqlConnection conn一样)把它放到一个使用块中:

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
  // put the code using ad here, ad is automatically disposed
}