如何确定 SqlDataAdapter 中是否返回零记录

发布于 2024-08-01 16:40:22 字数 575 浏览 3 评论 0原文

我有以下代码

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

非要怀念 2024-08-08 16:40:22

我认为问题不在 SqlDataAdapter 返回的记录中,因为即使它是空的也不会生成异常。
您查询中的问题,因为电子邮件字段是 varchar ,它应该是这样的:

SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = '" + userInfo.Email + "')", conn);

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:

SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = '" + userInfo.Email + "')", conn);
救赎№ 2024-08-08 16:40:22
dtResult1.Rows.Count > 0

-- 编辑

没有关注阅读该帖子; 请注意,您在 .Fill 上遇到了异常。 显然我这里的代码片段不会帮助你。 正如其他人所问的; 有什么例外吗?

-- 编辑:

并且,正如其他人所指出的,您的查询应该采用以下形式:

SqlCommand command = new SqlCommand(@"
select
    *
from
    EVENTSignUp
where
    MemberEmail = @MemberEmail
");

SqlParameter param = new SqlParameter("@MemberEmail", SqlDbType.NVarChar);
param.Value = userInfo.Email;

command.Parameters.Add(param);

SqlDataAdapter dtResult1 = new SqlDataAdapter(command);
DataTable dtResult1 = new DataTable();
SQLDataAdapter1.Fill(dtResult1);
dtResult1.Rows.Count > 0

-- 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:

SqlCommand command = new SqlCommand(@"
select
    *
from
    EVENTSignUp
where
    MemberEmail = @MemberEmail
");

SqlParameter param = new SqlParameter("@MemberEmail", SqlDbType.NVarChar);
param.Value = userInfo.Email;

command.Parameters.Add(param);

SqlDataAdapter dtResult1 = new SqlDataAdapter(command);
DataTable dtResult1 = new DataTable();
SQLDataAdapter1.Fill(dtResult1);
萤火眠眠 2024-08-08 16:40:22

您还可以使用SqlDataReader进行检查。 这是一个简单的示例,用于检查 SqlDataReader 是否有结果:

String con_string = "Your Connection String Here";
String strSQL = "Your Query Here";
using (SqlDataReader objNameDR = SQLHelper.ExecuteReader(con_string, CommandType.Text, strSQL, null)) {

    if (objNameDR.HasRows) {
        // Having Rows
    }
    else{
       // Having No Row (Zero Record)
    }

}

You can also check it by using SqlDataReader. Here is a simple example to check if SqlDataReader has result or not by this:

String con_string = "Your Connection String Here";
String strSQL = "Your Query Here";
using (SqlDataReader objNameDR = SQLHelper.ExecuteReader(con_string, CommandType.Text, strSQL, null)) {

    if (objNameDR.HasRows) {
        // Having Rows
    }
    else{
       // Having No Row (Zero Record)
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文