0

我正在我的项目上运行代码分析。我收到 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();
}  

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

4

1 回答 1

1

只需将例如添加ad.Dispose()到您的代码中。警告不是关于语法,而是关于缺少对 Dispoable 对象的调用(这可能导致内存泄漏和其他问题)。如果一个对象实现了IDisposable你可以把它放到一个 using 块中。与垃圾收集的关系在这里被揭开

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 块中:

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
  // put the code using ad here, ad is automatically disposed
}
于 2012-12-19T16:47:04.643 回答