c#编程:登录按钮,执行错误!

来源:百度知道 编辑:UC知道 时间:2024/05/26 03:19:57
我在做通讯录管理系统时,用以下代码,不知道什么地方出错了,编译没错,但执行不出来;望哪位高手大哥指点迷津:谢谢
private void btnin_Click(object sender, EventArgs e)
{

string connStr = "Data Source=F-PC\\YUE;Initial Catalog=message;user id=sa;password=121233";
string sql = string .Format ("select count(*) from yonghu where username='{0}', userpwd='{1}' and userlei='{2}'",txtname .Text ,txtpwd .Text ,comlei .Text );
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

SqlCommand cmd = new SqlCommand(sql,conn);
int count = (int)cmd.ExecuteScalar();
if (count == 1)
{

if (ValiInput())
{
if (comlei.Text == "用户")
{
Form3 xx = new Form3();

你说的执行不出来是什么情况 是什么都没有么?还是怎么样?

你确认你编译没出错吗?看看这里:
string sql = string .Format ("select count(*) from yonghu where username='{0}', userpwd='{1}' and userlei='{2}'",txtname .Text ,txtpwd .Text ,comlei .Text );

很明显txtname .Text ,txtpwd .Text ,comlei .Text,中间都有个空格,不知道是你粘贴到这里才出现的,还是你代码里本身就是这样的?

设置断点,命中断点之后按F11,一句一句的执行,就可以知道是哪里错了

你将if (ValiInput())改为if(!ValiInput())就可以了,你在验证中返回的是false,只有在true时才执行if(!ValiInput)中的代码