菜鸟求助。急!急!急!单机的登陆问题!!!!!!!!!!

jking1989 2009-11-09 09:41:11

private void button1_Click(object sender, EventArgs e)
{
string connstring="Data Source=.;Initial Catalog=new;Integrated Security=true;";
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("select count(*) from Tab where id='" + textBox1.Text+ "' and password='" + textBox2.Text + "'", conn))

{

if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("good");
}
else
{
MessageBox.Show("bad");
}
}
}
}


大家帮我看看啊,运行时可以运行,可总是显示bad,正确应该是good 啊
还有数据库为Sql,本地服务器,数据库名为new,表名为Tab,id和password类型为 int
大家帮我看看是什么问题啊!!!!!
拜托了
...全文
182 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪子-无悔 2009-11-09
  • 打赏
  • 举报
回复
@tchjl2007
密码一般都用字符类型
加密方式 一般都是 MD5或者3DES算法.
根据你个人的喜好了 .哈哈
Hamsic 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jking1989 的回复:]
引用 8 楼 w88463934 的回复:
因为cmd.ExecuteNonQuery() 返回的是影响行数,而查询语句是不会有任何影响行数的,所以不可能返回大于0,
应该使用SqlDataReader reader = cmd.ExecuteReader(),然后判断是否reader里面是否有数据
if(reader.read())
  ShowMessageBox("good");
else
  ShowMessageBox("bad");
或者使用cmd.ExecuteScal();

太感谢了,这样是对了
顺便再请教下,大家密码存在数据库是用 什么类型?

[/Quote]


字符 + mD5加密
jking1989 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 w88463934 的回复:]
因为cmd.ExecuteNonQuery() 返回的是影响行数,而查询语句是不会有任何影响行数的,所以不可能返回大于0,
应该使用SqlDataReader reader = cmd.ExecuteReader(),然后判断是否reader里面是否有数据
if(reader.read())
ShowMessageBox("good");
else
ShowMessageBox("bad");
或者使用cmd.ExecuteScal();
[/Quote]
太感谢了,这样是对了
顺便再请教下,大家密码存在数据库是用 什么类型?
yangqizhong 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 w88463934 的回复:]
因为cmd.ExecuteNonQuery() 返回的是影响行数,而查询语句是不会有任何影响行数的,所以不可能返回大于0,
应该使用SqlDataReader reader = cmd.ExecuteReader(),然后判断是否reader里面是否有数据
if(reader.read())
  ShowMessageBox("good");
else
  ShowMessageBox("bad");
或者使用cmd.ExecuteScal();
[/Quote]

整解 ,用ExecuteReader()
jking1989 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 vistakobe 的回复:]
错了,我,你的cmd.ExecuteNonQuery()错了。这个函数是增、删、改。、
cmd.ExecuteReader()才对。

汗啊!呵呵
[/Quote]
cmd.ExecuteReader() 这个在这要怎么用呢,请教下,用下去有错误
w88463934 2009-11-09
  • 打赏
  • 举报
回复
因为cmd.ExecuteNonQuery() 返回的是影响行数,而查询语句是不会有任何影响行数的,所以不可能返回大于0,
应该使用SqlDataReader reader = cmd.ExecuteReader(),然后判断是否reader里面是否有数据
if(reader.read())
ShowMessageBox("good");
else
ShowMessageBox("bad");
或者使用cmd.ExecuteScal();
sd1840888 2009-11-09
  • 打赏
  • 举报
回复
convent.toInt32(textBox1.text.trim())
convent.toInt32(textBox2.text.trim())
VistaKobe 2009-11-09
  • 打赏
  • 举报
回复
错了,我,你的cmd.ExecuteNonQuery()错了。这个函数是增、删、改。、
cmd.ExecuteReader()才对。

汗啊!呵呵
jking1989 2009-11-09
  • 打赏
  • 举报
回复
我的是SQL2005
本地服务器用 一个 .不就可以了吗
还需要 用户名密码吗?
VistaKobe 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liherun 的回复:]
"server=server2008;database=;uid=;pwd="
[/Quote]

他用的是集成登录。

你试着改下"select * from tab where id= "+textBox1.text.trim()+" and password = "+textBox2.text.trim()";

两个参数不都是int吗?
liherun 2009-11-09
  • 打赏
  • 举报
回复
"server=server2008;database=;uid=;pwd="
liherun 2009-11-09
  • 打赏
  • 举报
回复
你数据库的用户名和密码呢?
jediael_lu 2009-11-09
  • 打赏
  • 举报
回复
不知道,看看。

111,097

社区成员

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

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

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