有人可以解释为什么Clear()
或Dispose()
无法刷新DataTable记录集吗?
我在MVC 4项目中使用DataTable,如var tbl = new DataTable()
这样可以很好地检索数据,但出于某些原因我使用tbl.Clear()
和tbl.Dispose()
然后尝试重用tbl
它仍然具有上一个查询的记录集,为什么?
有人可以告诉我如何刷新/清除/清空以前的DataTable记录集吗?
非常感谢任何帮助: - )
请参阅下面的示例代码:
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();
}
答案 0 :(得分:1)
似乎你总是使用相同的SqlDataAdapter
。
您创建了一个新命令:
cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
但你必须使用这个新命令设置userRecord:
userRecord = new SqlDataAdapter(cmd);
答案 1 :(得分:0)
以下是我修改过的代码,它解决了我的问题。看来我没有调用第二个存储过程,因此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();