在C#中怎样接受SQL 返回select受影响的行数

woguoguo 2008-02-20 10:59:55
select count(*) from biao where name=张三"
可是在C#中怎样接受SQL 返回select受影响的行数??
...全文
5204 53 打赏 收藏 转发到动态 举报
写回复
用AI写文章
53 条回复
切换为时间正序
请发表友善的回复…
发表回复
Euarne 2012-03-19
  • 打赏
  • 举报
回复
ExecuteNonQuery()用于select时返回值为-1
[MSDN]对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。如果正在执行插入或更新操作的表上存在触发器,则返回值包括受插入或更新操作影响的行数以及受一个或多个触发器影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。

[Quote=引用 16 楼 woguoguo 的回复:]

学习中 按照你的方法int rows = cmd.ExecuteNonQuery(); 返回的都是-1啊
[/Quote]
Euarne 2012-03-19
  • 打赏
  • 举报
回复
嘿嘿~~~之前还不知道ExecuteNonQuery()不能用于select...知道了后在纠结用那个...[Quote=引用 10 楼 gt2007good 的回复:]

int count = Convert.ToInt32(cmd.ExecuteScalar());
[/Quote]
tiger_yxq 2011-08-14
  • 打赏
  • 举报
回复
学习了
二J 2009-07-16
  • 打赏
  • 举报
回复
好多人都是错的,害我在半天
Chen先森o_o 2009-02-03
  • 打赏
  • 举报
回复
37楼是正解。正好遇到这个小问题。通过;
我的代码:
public int CheckLogin(string name, string pwd)
{
string str = "Integrated Security=SSPI;Initial Catalog=StudentManagerInf;Data Source=(local)";
SqlConnection conn = new SqlConnection(str);

try
{
conn.Open();

SqlCommand cmd = new SqlCommand(string.Format("select count(*) from userinf where username= '{0}' and password= '{1}'", name, pwd), conn);

SqlDataReader read = cmd.ExecuteReader();
i = 0;
while (read.Read())
{
i++;
}

return i;


}
catch (Exception ex)
{
conn.Close();
return i=0;

}
finally { conn.Close(); }
}
czaoth 2008-02-25
  • 打赏
  • 举报
回复
10楼正解
Dennis0529 2008-02-25
  • 打赏
  • 举报
回复
cmd.ExecuteScalar()
然后就可以得到返回行数
LRG315 2008-02-24
  • 打赏
  • 举报
回复
LZ要取这个行数,好像有点多此一举,不知LZ的用意何处?
hxn8203 2008-02-24
  • 打赏
  • 举报
回复
上句的"处理错了,"update table1 set col1='ddd' where col1='ddd'"
hxn8203 2008-02-24
  • 打赏
  • 举报
回复
ExecuteScalar 此方法在 .NET Framework 2.0 版中是新增的。
对于1。0,可以使用变通的方法。
比如对于 select * from table1 where col1="ddd"
想返回它的行数,可以把Select 语句修改成 update table1 set col1="ddd" where col1="ddd"




SqlCommand command = new SqlCommand(
"update table1 set col1="ddd" where col1=\"ddd\"", connection);
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
执行这条update不会修改表中的值,但当记录很多时,不知道会不会影响性能。
cxfcxj 2008-02-23
  • 打赏
  • 举报
回复
cmd.ExecuteNonQuery();只能用于返回insert,delect,update.查询语句影响的行数,
要用select查询受影响的行数,你必须用变通的方法,例如:
OleDbCommand command = new OleDbCommand("select count(*) as num from biao where name='张三'",conn);
OleDbDataReader reader = command.ExecuteReader();

if (reader.Read())
{
return Int16.Parse(reader["num"].ToString());
}
reader.Close();
taozi92 2008-02-22
  • 打赏
  • 举报
回复
15楼是正解 完全正确!com.ExecuteNonQuery()是执行而不返回行数,cmd.ExecuteScalar()是返回行数。
jfan288 2008-02-22
  • 打赏
  • 举报
回复
楼主牛逼啊。
不管你select什么,都是返回一个表啊,你再从表里读第一行第一列的数据不就成了吗?
zhaoguoqingluntan 2008-02-20
  • 打赏
  • 举报
回复
object o =com.ExecuteScalar();
return o.toString();
wzy_love_sly 2008-02-20
  • 打赏
  • 举报
回复
public DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}

DataSet ds = new DataSet();
ds = Query("select count(1) as [count] from tbname");
int count = int.Parse(ds.Tables[0]["count"]);
这不行?
woguoguo 2008-02-20
  • 打赏
  • 举报
回复
我知道啊 所以我才想知道select的情况下怎么返回受影响的行数
wzy_love_sly 2008-02-20
  • 打赏
  • 举报
回复
对于 Update、Insert 和 Delete 语句,返回值为该命令所影响的行数。
对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。
明白了
woguoguo 2008-02-20
  • 打赏
  • 举报
回复
int count = Convert.ToInt32(cmd.ExecuteScalar()); 这样返回的都是0啊
vrhero 2008-02-20
  • 打赏
  • 举报
回复
10楼正解...select语句用ExecuteNonQuery方法返回的总是-1...
woguoguo 2008-02-20
  • 打赏
  • 举报
回复
学习中 按照你的方法int rows = cmd.ExecuteNonQuery(); 返回的都是-1啊
加载更多回复(32)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧