我正在我的项目上运行代码分析。我收到 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();
}
关于如何解决这些错误的任何想法?谢谢