0

プロジェクトでコード分析を実行しています。警告が 8 件、エラーが 0 件です。それらが何を意味するのかわかりませんが、同じコード (CA2000) である 6 つと、同じコード (CA2240) である他の 2 つがあります。これは一般的な警告ですか?

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

コードに eg を追加するだけ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 に答える