command.ExecuteNonQuery()为什么总是返回-1

Frank_Mt 2011-08-23 04:15:17
command.ExecuteNonQuery()为什么总是返回-1

string sqlconn = "Data Source=" + txtDbServer.Text + ";user id=" + txtDbUserName.Text + ";password=" + txtDbPassword.Text + ";initial catalog=" + txtDbName.Text;
SqlConnection conn = new SqlConnection(sqlconn);
try
{
conn.Open();
string sqlString = "select * from USERMST where UserName='" + txtLoginName.Text + "' and Password =" + txtLoginPassword.Text;
SqlCommand command = new SqlCommand(sqlString, conn);

command.CommandType = CommandType.Text;

int row = command.ExecuteNonQuery();

if (row > 0)
{
MessageBox.Show("登录成功!", "提示");
}
else
{
MessageBox.Show("登录失败!", "警告");
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
...全文
229 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
细嗅蔷薇 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]

1. 你的password也应该用单引号括起来。
2. ExecuteNonQuery 是用来执行 Update, Delete, Insert 的Sql语句。
select 应该用 ExecuteReader() 或者用 select count(*) from ... 结合 ExecuteScalar()
[/Quote]
引用
子夜__ 2011-08-23
  • 打赏
  • 举报
回复
string sqlString = "select * from USERMST where UserName='" + txtLoginName.Text + "' and Password =" + txtLoginPassword.Text;

查找
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
xiongxyt2 2011-08-23
  • 打赏
  • 举报
回复
希望你能认真看高手给你回的贴,
一楼JustACoder大哥不已经给你回答了吗,ExecuteNonQuery用于增删改
查询是否存在(就你现在这个判断用户是否存在)用ExecuteScalar()
Frank_Mt 2011-08-23
  • 打赏
  • 举报
回复
我用 ExecuteScalar()已经成功了

ExecuteNonQuery :int SqlCommand.ExcuteNonQuery() 对连接执行 Transact-SQL语句并执行返回受影响的行数。
ExecuteNonQuery是返回受影响的行数,为何不能在这里使用?
zhou_xuexi 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 frank_mt 的回复:]
command.ExecuteNonQuery()为什么总是返回-1

string sqlconn = "Data Source=" + txtDbServer.Text + ";user id=" + txtDbUserName.Text + ";password=" + txtDbPassword.Text + ";initial catalog=" + txtDbName.Text;……
[/Quote]正解
Robin 2011-08-23
  • 打赏
  • 举报
回复
建议使用Parameter传参方式
System.Data.SqlClient.SqlParameter
半瓶神仙水 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lmaohuanl 的回复:]
sql语句有误

引用楼主 frank_mt 的回复:
command.ExecuteNonQuery()为什么总是返回-1

string sqlconn = "Data Source=" + txtDbServer.Text + ";user id=" + txtDbUserName.Text + ";password=" + txtDbPassword.Text + ";initi……
[/Quote]正解
白玉寒天 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]
1. 你的password也应该用单引号括起来。
2. ExecuteNonQuery 是用来执行 Update, Delete, Insert 的Sql语句。
select 应该用 ExecuteReader() 或者用 select count(*) from ... 结合 ExecuteScalar()
[/Quote]
正解
LMAOhuaNL 2011-08-23
  • 打赏
  • 举报
回复
sql语句有误
[Quote=引用楼主 frank_mt 的回复:]
command.ExecuteNonQuery()为什么总是返回-1

string sqlconn = "Data Source=" + txtDbServer.Text + ";user id=" + txtDbUserName.Text + ";password=" + txtDbPassword.Text + ";initial catalog=" + txtDbName.Text;……
[/Quote]
ycproc 2011-08-23
  • 打赏
  • 举报
回复
受影响的行数是-1是增 了
weike021996 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]

1. 你的password也应该用单引号括起来。
2. ExecuteNonQuery 是用来执行 Update, Delete, Insert 的Sql语句。
select 应该用 ExecuteReader() 或者用 select count(*) from ... 结合 ExecuteScalar()
[/Quote]引用
小阳 2011-08-23
  • 打赏
  • 举报
回复
把你的sql语句改一下

string sql=string.Format("insert.....'{0}','{1}'",name,password);
机器人 2011-08-23
  • 打赏
  • 举报
回复
1. 你的password也应该用单引号括起来。
2. ExecuteNonQuery 是用来执行 Update, Delete, Insert 的Sql语句。
select 应该用 ExecuteReader() 或者用 select count(*) from ... 结合 ExecuteScalar()

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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