我试图保留原始 DataTable 的副本,同时尝试截断原始数据表以用于其他目的。但是当我截断原件时,副本也会被截断。
using (SqlCommand cmd = new SqlCommand(sqlSelect, conn))
{
//Set Time to minute and a half, should not take longer than that!
cmd.CommandTimeout = 90;
DataTable dt = new DataTable();
DataTable copyDt = new DataTable();
//Check the state of the database connection. If closed, then open it.
if (conn.State != ConnectionState.Open)
conn.Open();
dt.Load(cmd.ExecuteReader());
copyDt = dt.Copy();
if (Session["ServiceRequestSearch"] != null)
{
((DataTable)Session["ServiceRequestSearch"]).Clear();
((DataTable)Session["ServiceRequestSearch"]).Dispose();
Session.Remove("ServiceRequestSearch");
}
Session["ServiceRequestSearch"] = copyDt;
if (dt.Rows.Count > take)
for (int i = take; i < dt.Rows.Count; i++)
dt.Rows[i].Delete();
dt.AcceptChanges();
TextBox.Text = dt.Rows.Count + "";
TextBox2.Text = ((DataTable)Session["ServiceRequestSearch"]).Rows.Count + "";
//Close SQL data connection if it is still open.
if (conn.State != ConnectionState.Closed)
conn.Close();
}
它们都显示相同的行数,即使副本应该高得多。