SQlDataReader 不更新其结果集
我有一个用以下命令填充的 sqldatareader:
string cmdlookup = String.Format("Select {0} from {1}", strSourceName, strSourceTable);
SqlCommand sqlcom = new SqlCommand(cmdlookup, con);
SqlDataReader rdrsource = cmd.ExecuteReader();
while (rdrsource.Read())
{
this.lstSourceValues.Add(System.Convert.ToString(rdrsource[strSourceName]));
}
rdrsource.Close();
程序中其他地方有一个函数可以修改此表。我已经验证它确实更新了有问题的表。该表现在有 11 个值。
此查询的结果永远不会改变。它始终返回 5 行,即使应用程序中其他位置的函数已明确更新了表。下面几行我也关闭了连接。
这个东西是以某种方式缓存的吗?为什么查询不更新?
I have a sqldatareader populated with this command:
string cmdlookup = String.Format("Select {0} from {1}", strSourceName, strSourceTable);
SqlCommand sqlcom = new SqlCommand(cmdlookup, con);
SqlDataReader rdrsource = cmd.ExecuteReader();
while (rdrsource.Read())
{
this.lstSourceValues.Add(System.Convert.ToString(rdrsource[strSourceName]));
}
rdrsource.Close();
There is a function elsewhere in the program to modify this table. I have verified that it does indeed update the table in question. The table now has 11 values.
This results of this query NEVER CHANGES. It always returns 5 rows, even though a function elsewhere in the application has clearly updated the table. A few lines below I close the connection, too.
Is this thing cached somehow? Why doesn't the query update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够解决这个问题。问题是,当我调用 .Close() 和/或 .Dispose() 时,它并没有真正摆脱该对象。
相反,我需要切换到使用 using 结构。由于某种原因,当我使用 using 构造时,它肯定会关闭并删除该对象。
它没有改变,因为我不断获得对象的相同内存表示。
I was able to resolve the issue. The problem was that when I was calling .Close() and/or .Dispose(), it wasn't really getting rid of the object.
Instead, I needed to switch to using a using construct. For some reason, when I used the using construct, it definitely closed deleted the object.
It didn't change because I was continually getting the same in-memory representation of the object.