C# step by step

begin programme
posts - 2, comments - 8, trackbacks - 0, articles - 0

C#用户窗体登陆设计

Posted on 2006-10-26 19:57 开始编程 阅读(6107) 评论(6)  编辑 收藏 引用

做为学习过程中的札记 所写的都是本人自己的一些设计写法 只是想记录一下学习的点点嘀嘀。
转入正题:利用数据库提取用户验证信息实现登陆。
priviate void button1_Click(object sender,System.EventArgs e)
{
       SqlConnection conn=new SqlConnection("@Data Source="(local);Integrated Security=SSPI;Initial Catalog=DBApp"");
       SqlCommand cmd=conn.CreateCommand();
       cmd.CommandType=CommandType.Text;
     cmd.CommandType = CommandType.Text;
            if (txtUserName.Text.Length == 0)
            {
                MessageBox.Show("用户不能为空,请查询后输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select count(*) from UserList where UserName='" + txtUserName.Text + "'";
            conn.Open();
            if ((int)cmd.ExecuteScalar() == 0)
            {
                MessageBox.Show("不存在此用户,请检查后重新输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select UserName, PassWord from UserList where PassWord='" +txtPassWord .Text + "'";
            SqlDataReader myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
                MessageBox.Show("登录成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmMain fm = new frmMain();
                fm.ShowDialog();
            }

            else
            {
                MessageBox.Show("用户" + txtUserName.Text + "的密码不正确", "密码", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                txtPassWord.Focus();
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
}

Feedback

# re: C#用户窗体登陆设计  回复  更多评论   

2006-11-01 14:42 by e商
不错。

# re:   回复  更多评论   

2006-11-01 16:08 by 天天影院
很详细,谢谢啊

# re: C#用户窗体登陆设计  回复  更多评论   

2007-05-22 14:32 by afas
SB

# re: C#用户窗体登陆设计  回复  更多评论   

2008-02-25 09:08 by 无聊
晕,代码写这样。。。

有没有测试?

# re: C#用户窗体登陆设计  回复  更多评论   

2009-09-02 16:37 by 无聊
好多错误

# re: C#用户窗体登陆设计  回复  更多评论   

2011-05-18 10:41 by 阿萨德
if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
C#不让用&&这个吧
只有注册用户登录后才能发表评论。