如何确定 SqlDataAdapter 中是否返回零记录
我有以下代码
string connString = ConfigurationManager.ConnectionStrings["XXXConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = " + userInfo.Email + ")", conn);
DataTable dtResult1 = new DataTable();
SQLDataAdapter1.Fill(dtResult1);
,但如果没有返回记录,我只会得到一个异常:
SQLDataAdapter1.Fill(dtResult1);
如何确定此查询是否没有返回记录?
i have the following code
string connString = ConfigurationManager.ConnectionStrings["XXXConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = " + userInfo.Email + ")", conn);
DataTable dtResult1 = new DataTable();
SQLDataAdapter1.Fill(dtResult1);
but if there are no records returned, i simply get an exception at:
SQLDataAdapter1.Fill(dtResult1);
how do i determine if there are no records returned from this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题不在 SqlDataAdapter 返回的记录中,因为即使它是空的也不会生成异常。
您查询中的问题,因为电子邮件字段是 varchar ,它应该是这样的:
I think the problem not in records returend by SqlDataAdapter because even if it's empty it will not generate exception.
the problem in your query because email field is varchar and it should be like this:
-- 编辑
没有关注阅读该帖子; 请注意,您在 .Fill 上遇到了异常。 显然我这里的代码片段不会帮助你。 正如其他人所问的; 有什么例外吗?
-- 编辑:
并且,正如其他人所指出的,您的查询应该采用以下形式:
-- edit
Didn't follow read the post; notice you are getting an exception on .Fill. Obviously my code snippet here will not help you with that. As others have asked; what is the exception?
-- edit:
And, as others have noted, your query should be of the form:
您还可以使用
SqlDataReader
进行检查。 这是一个简单的示例,用于检查SqlDataReader
是否有结果:You can also check it by using
SqlDataReader
. Here is a simple example to check ifSqlDataReader
has result or not by this: