2012-12-19 2 views
0

내 프로젝트에서 코드 분석을 실행하고 있습니다. 나는 8 개의 경고와 0 개의 오류를 얻고있다. 나는 그들이 의미하는 바를 모르지만 6 개는 동일한 코드 (CA2000)이고 다른 2 개는 동일한 코드 (CA2240)입니다. 이것은 일반적인 경고입니까?VS2010의 프로젝트에서 코드 분석 실행

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

그냥 추가하십시오. ad.Dispose()을 코드에 추가하십시오. 경고는 구문과 관련이 없지만 missing call to Dispoable objects에 관한 것입니다 (메모리 누수 및 기타 문제가 발생할 수 있음). 객체가 IDisposable을 구현하면 using 블록에 넣을 수 있습니다. 가비지 콜렉션에 대한 관계는 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 
} 
+0

1 만 user'는 감사 돌아 오지 않을 이유'궁금 당신을 도와주세요. – jp2code