为的ExecuteNonQuery SELECT sql语句没有返回行 [英] ExecuteNonQuery for SELECT sql statement returning no rows

查看:178
本文介绍了为的ExecuteNonQuery SELECT sql语句没有返回行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么检查没有行返回的ExecuteNonQuery用于选择SQL语句不返回行??后

how do you check for no rows returned after ExecuteNonQuery for SELECT sql statement returns no rows??

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-ca/library/system.data.common.dbcommand.executenonquery.aspx\"><$c$c>ExecuteNonQuery方法 返回受要么是插入,一个更新行(S)的数量删除。这种方法是将用于执行DML(数据操纵语言)语句如所述previously

The ExecuteNonQuery Method returns the number of row(s) affected by either an INSERT, an UPDATE or a DELETE. This method is to be used to perform DML (data manipulation language) statements as stated previously.

ExecuteReader方法 会返回的结果集 SELECT 的。这种方法是,当要查询为一束的结果,要被用于诸如从表中的行,视图,不管

The ExecuteReader Method will return the result set of a SELECT. This method is to be used when you're querying for a bunch of results, such as rows from a table, view, whatever.

的<一个href=\"http://msdn.microsoft.com/en-ca/library/system.data.common.dbcommand.executescalar.aspx\"><$c$c>ExecuteScalar方法 将第一排,从 SELECT 语句第一列返回一个值。这种方法是用来当你想到只有一个值从要返回的查询。

The ExecuteScalar Method will return a single value in the first row, first column from a SELECT statement. This method is to be used when you expect only one value from the query to be returned.

总之,这是正常的,你在使用的的ExecuteNonQuery 方式都没有从 SELECT 语句的结果。使用的ExecuteReader 来代替。使用的ExecuteReader 方法,必将了解多少行通过 SqlDataReader的对象的实例返回返回。

In short, that is normal that you have no results from a SELECT statement while using the ExecuteNonQuery method. Use ExecuteReader instead. Using the ExecuteReader method, will will get to know how many rows were returned through the instance of the SqlDataReader object returned.

int rows = 0;

if (reader.HasRows)
    while (reader.Read())
        rows++;

return rows; // Returns the number of rows read from the reader.

这篇关于为的ExecuteNonQuery SELECT sql语句没有返回行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆