MVC 4刷新/清空/清除数据表
本文关键字:清除 数据表 清空 MVC 刷新 | 更新日期: 2023-09-27 18:09:58
有人能解释为什么Clear()
或Dispose()
不能刷新数据表记录集吗?
我在MVC 4项目中使用数据表,如var tbl = new DataTable()
,这对检索数据很好,但出于某种原因,当我使用tbl.Clear()
和tbl.Dispose()
,然后尝试重用tbl
时,它仍然有以前查询的记录集,为什么?
有人能让我知道如何冲洗/清除/清空以前的数据表记录集吗?
任何帮助都将非常感激:-)
请参见下面的示例代码:
string name = "";
string password = "";
var stringWriter = new StringWriter();
var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
var tbl = new DataTable();
string strConn = WebConfigurationManager.ConnectionStrings["TestConnection"].ToString();
var conn = new SqlConnection(strConn);
using (conn)
{
conn.Open();
var cmd = new SqlCommand("GetUserLoginID", conn) {CommandType = CommandType.StoredProcedure};
cmd.Parameters.AddWithValue("@UID", uid);
cmd.Parameters.AddWithValue("@BID", bId);
var userRecord = new SqlDataAdapter(cmd);
userRecord.Fill(tbl);
conn.Close();
if (tbl.Rows.Count > 0)
{
var row = tbl.Rows[0];
name = row["Name"].ToString();
password = row["Password"].ToString();
code = row["Code"].ToString();
}
tbl.Clear();
tbl.Dispose();
cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("@Code", code);
cmd.Parameters.AddWithValue("@BID", bId);
userRecord.Fill(tbl);
if (tbl.Rows.Count > 0)
{
var row = tbl.Rows[0];
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteElementString("BID", row["BID"].ToString());
writer.WriteElementString("Name", name);
}
else
{
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteElementString("BID", "Error");
}
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
似乎你总是使用相同的SqlDataAdapter
。您创建了一个新命令:
cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
但是你必须用这个新命令来设置userRecord:
userRecord = new SqlDataAdapter(cmd);
下面是我修改的代码,它解决了我的问题。看起来我没有调用第二个存储过程,因此DataTable tbl
没有被第二个数据集填充。
string name = "";
string password = "";
var stringWriter = new StringWriter();
var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
var tbl = new DataTable();
string strConn = WebConfigurationManager.ConnectionStrings["TestConnection"].ToString();
var conn = new SqlConnection(strConn);
using (conn)
{
conn.Open();
var cmd = new SqlCommand("GetUserLoginID", conn) {CommandType = CommandType.StoredProcedure};
cmd.Parameters.AddWithValue("@UID", uid);
cmd.Parameters.AddWithValue("@BID", bId);
var userRecord = new SqlDataAdapter(cmd);
userRecord.Fill(tbl);
if (tbl.Rows.Count > 0)
{
var row = tbl.Rows[0];
name = row["Name"].ToString();
password = row["Password"].ToString();
code = row["Code"].ToString();
}
tbl.Clear();
cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("@Code", code);
cmd.Parameters.AddWithValue("@BID", bId);
userRecord = new SqlDataAdapter(cmd);
userRecord.Fill(tbl);
if (tbl.Rows.Count > 0)
{
var row = tbl.Rows[0];
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteElementString("BID", row["BID"].ToString());
writer.WriteElementString("Name", name);
}
else
{
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteElementString("BID", "Error");
}
writer.WriteEndElement();
writer.Flush();
writer.Close();
conn.Close();
}
tbl.Dispose();