WinForm实现用户注册登录Demo

 简单运行效果如下:

首先创建好数据库和表:

--切换到MyDataBase01;
use MyDataBase01;

--创建数据库表
create table ADO.LoginTbl
(
   loginId int identity(1,1) not null,
   loginUid nvarchar(20) not null,
   loginPwd varchar(16) not null
);
alter table ADO.LoginTbl
add
constraint [PK_ADO.LoginTbl_loginId] primary key nonclustered (loginId),
constraint [UQ_ADO.LoginTbl_loginUid] unique(loginUid),
constraint [CK_ADO.LoginTbl_loginPwd] check(len(loginPwd)>=6);


select * from ADO.LoginTbl;

insert into ADO.LoginTbl(loginUid,loginPwd) values('张三',123456);

select COUNT(*) from ADO.LoginTbl where loginUid='张三';

 

C#代码部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 登陆界面
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txtUerName_Leave(object sender, EventArgs e)
        {
            string uid = txtUerName.Text.Trim();
            if (uid.Length == 0)
            {
                lblNote.ForeColor = Color.Red;
                lblNote.Text = "用户名未填写";
                return;
            }

            string connStr = @"server=.;database=MyDataBase01;uid=sa;pwd=123456;";
            string sql = "select COUNT(*) from ADO.LoginTbl where loginUid='"+uid+"';";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql,conn))
                {
                    conn.Open();
                    int res = (int)cmd.ExecuteScalar();
                    if (res > 0)
                    {
                        lblNote.ForeColor = Color.Red;
                        lblNote.Text = "X,用户名已存在!";
                    }
                    else
                    {
                        lblNote.ForeColor = Color.Green;
                        lblNote.Text = "√,用户名可用";
                    }
                }

            }

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            string uid = txtUerName.Text.Trim();
            string pwd1 = txtPassword.Text;
            string pwd2 = txtRePassword.Text;

            if (uid.Length==0||pwd1.Length==0||pwd2.Length==0)
            {
                MessageBox.Show("请填写完整信息");
                return;
            }

            if (pwd1 != pwd2)
            {
                MessageBox.Show("密码不一致!");
                return;
            }

            if (lblNote.Text == "X,用户名已存在!")
            {
                MessageBox.Show("用户名已存在,请重新输入!");
                return;
            }


            string connStr = @"server=.;database=MyDataBase01;uid=sa;pwd=123456;";

            string sql = string.Format("insert into ADO.LoginTbl(loginUid,loginPwd) values('{0}','{1}');",
                uid,
                pwd1);
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    int res = cmd.ExecuteNonQuery();
                    if (res > 0)
                    {
                        MessageBox.Show("恭喜你!注册成功!");
                    }
                    else
                    {
                        MessageBox.Show("杯具!~~>_<~~,注册失败!");
                    }
                }

            }


        }


    }
}

 

源码下载:ADO.NET实现用户注册登录.rar

 

posted @ 2013-04-21 12:03  IT浪潮之巅  阅读(1551)  评论(1编辑  收藏  举报
   友情链接: 淘宝优惠券