简体   繁体   English

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

[英]Run Code Analysis on project in VS2010

I am running the code Analysis on my project. 我正在我的项目上运行代码分析。 I am getting 8 warnings and 0 errors. 我收到8警告和0错误。 I am not sure what they mean but I have 6 that are the same code (CA2000) and the other 2 are the same code (CA2240). 我不确定它们的含义,但是我有6个相同的代码(CA2000),其他2个相同的代码(CA2240)。 Is this a common warning? 这是常见警告吗?

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.

I have the "using" but I am still getting that warning. 我有“正在使用”的提示,但仍收到该警告。 Is my syntax incorrect? 我的语法不正确吗?

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

Any ideas on how to fix these errors? 关于如何解决这些错误的任何想法? Thanks 谢谢

Just add eg ad.Dispose() to your code. 只需将ad.Dispose()添加到您的代码中即可。 The warning is not about the syntax but it is about a missing call to Dispoable objects (and that can lead into memory leaks and other problems). 该警告不是关于语法的,而是关于对Dispoable对象缺少调用 (这可能导致内存泄漏和其他问题)。 If an objects implements IDisposable you can put it into a using block. 如果对象实现IDisposable ,则可以将其放入using块。 The relation to garbage collection is 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();
}

... or much better (as you already did with SqlConnection conn) put it into a using block: ...或更好的方法(就像您已经使用SqlConnection conn所做的那样)将其放入using块中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM