C#登陆界面求助,为什么我就是读不出数据出来!!

a6248951 2013-07-23 01:39:15

private void button1_Click(object sender, EventArgs e)
{
String userid;

String userpassword;

userid = this.textBox1.Text;

userpassword = this.textBox2.Text;

if (userid == "" || userpassword == "")
{

MessageBox.Show("请确认您的输入!");

}
try
{
SqlConnection conn = new SqlConnection("server=.;database=Loading;Trusted_Connection=true");
SqlCommand cmd = new SqlCommand("select count(*) from Loa where 用户名='" + userid + "'and 密码='" + userpassword + "'", conn);
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (count>0)
{
main form = new main();
form.MdiParent = this;
form.Show();
this.Hide();
}
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show("错误" + ex.ToString(), "错误");
}

}
...全文
330 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
efaye_yang 2013-07-23
  • 打赏
  • 举报
回复
读取不到数据有很多:1、参数获取的值有问题:2、连接数据库出错;3、查询参数出错;4、本来就本数据,顺着思路一个一个查看就可以了
se7en 2013-07-23
  • 打赏
  • 举报
回复
调试就可以解决 。总要有报错吧 。不能什么都没有吧
maysoft 2013-07-23
  • 打赏
  • 举报
回复
都回答了。。。
ygnmn 2013-07-23
  • 打赏
  • 举报
回复
引用 5 楼 gy348 的回复:
提示什么错误? 另外把 SqlConnection conn = new SqlConnection("server=.;database=Loading;Trusted_Connection=true"); 改成 SqlConnection conn = new SqlConnection("server=;database=Loading;uid=sa;pwd=密码");
同意楼上说法
  • 打赏
  • 举报
回复
登录数据库的用户名和密码没有
打杂的小小柯 2013-07-23
  • 打赏
  • 举报
回复
而且 SqlCommand cmd = new SqlCommand("select count(*) from Loa where 用户名=N'" + userid + "'and 密码='" + userpassword + "'", conn); 中的and我觉得应该需要在其前面有个空格
打杂的小小柯 2013-07-23
  • 打赏
  • 举报
回复
textBox1.Text的文本是中文?是的话我觉得应该在
SqlCommand cmd = new SqlCommand("select count(*) from Loa where 用户名='" + userid + "'and 密码='" + userpassword + "'", conn);
这句中的用户名=后加上N,即变成
SqlCommand cmd = new SqlCommand("select count(*) from Loa where 用户名=N'" + userid + "'and 密码='" + userpassword + "'", conn);
gy348 2013-07-23
  • 打赏
  • 举报
回复
提示什么错误? 另外把 SqlConnection conn = new SqlConnection("server=.;database=Loading;Trusted_Connection=true"); 改成 SqlConnection conn = new SqlConnection("server=;database=Loading;uid=sa;pwd=密码");
lqy_1111 2013-07-23
  • 打赏
  • 举报
回复
你数据库和你所取的表是否正确的?估计是你对数据库没有正确操作
全栈极简 2013-07-23
  • 打赏
  • 举报
回复
数据库连接字符串不对、没有uid=sa;pwd=123456
kxyzjm62 2013-07-23
  • 打赏
  • 举报
回复
你从数据库读数据了吗?
stonespace 2013-07-23
  • 打赏
  • 举报
回复
是什么错?发生异常吗?
fsstolw 2013-07-23
  • 打赏
  • 举报
回复
同意7楼的,应该在and前面加一个空格就可以
江湖评谈 2013-07-23
  • 打赏
  • 举报
回复
上面都回答了,没我事
相思梦 2013-07-23
  • 打赏
  • 举报
回复
你看看你的数据库连对没...

110,535

社区成员

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

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

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