C#基础用户登陆



1.主界面代码:


2.注册页面






3.登陆界面



登陆注册代码:
//编写登录界面逻辑
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; namespace LoginDatabase
{
public partial class Login : Form
{
private int errorTime = ; public Login() {
InitializeComponent();
} private void loginBtn_Click(object sender, EventArgs e) {
errorTime = errorTime - ; string username = txtName.Text.Trim(); //取出账号
string pw = txtPwd.Text.Trim(); //取出密码
string constr = "Server=.;DataBase=SU; Integrated Security=True"; //设置连接字符串
SqlConnection mycon = new SqlConnection(constr); //实例化连接对象
mycon.Open(); SqlCommand mycom = mycon.CreateCommand(); //创建SQL命令执行对象
string s1 = "select account,password from register where account='" + username + "' and password='" + pw + "'"; //编写SQL命令
mycom.CommandText = s1; //执行SQL命令
SqlDataAdapter myDA = new SqlDataAdapter(); //实例化数据适配器
myDA.SelectCommand = mycom; //让适配器执行SELECT命令
DataSet myDS = new DataSet(); //实例化结果数据集
int n = myDA.Fill(myDS, "register"); //将结果放入数据适配器,返回元祖个数
if (n != ) {
if (checkCode.Text == textCheck.Text) {
MessageBox.Show("欢迎使用!"); //登录成功
this.Close();
} else {
MessageBox.Show("验证码填写错误");
textCheck.Text = "";
}
} else
if (errorTime < ) {
MessageBox.Show("用户名或密码有错。请重新输入!还有" + errorTime.ToString() + "次机会");
txtName.Text = ""; //清空账号
txtPwd.Text = ""; //清空密码?
txtName.Focus(); //光标设置在账号上
} else {
MessageBox.Show("你输入的用户名或密码已达三次? 将退出程序");
this.Close();
} } private void cancelBtn_Click(object sender, EventArgs e) {
Application.Exit();
} private void button1_Click(object sender, EventArgs e) {
Register register = new Register();
register.ShowDialog();
} private void checkCode_Click(object sender, EventArgs e) {
Random random = new Random();
int minV = , maxV = ;
checkCode.Text = random.Next(minV, maxV).ToString();
}
}
} .编写注册界面逻辑
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace LoginDatabase
{
public partial class Register : Form
{
public Register() {
InitializeComponent();
} private void btnRegister_Click(object sender, EventArgs e) { //检查是否已经存在
string userID = userId.Text.Trim(); //取出账号 /**
* 连接数据库
*/
string constr = "Server=.;DataBase=SU; Integrated Security=True"; //设置连接字符串
SqlConnection mycon = new SqlConnection(constr); //实例化连接对象
mycon.Open(); //查询新注册的用户是否存在
SqlCommand checkCmd = mycon.CreateCommand(); //创建SQL命令执行对象
string s = "select account from register where account='" + userID + "'";
checkCmd.CommandText = s;
SqlDataAdapter check = new SqlDataAdapter(); //实例化数据适配器
check.SelectCommand = checkCmd; //让适配器执行SELECT命令
DataSet checkData = new DataSet(); //实例化结果数据集
int n = check.Fill(checkData, "register"); //将结果放入数据适配器,返回元祖个数
if (n != ) {
MessageBox.Show("用户名存在");
userId.Text = ""; userPw.Text = "";
nickName.Text = "";
} //确认密码
if (ensurePw.Text != userPw.Text) {
ensurePw.Text = "";
} //验证码
if (textCheck.Text != checkCode.Text) {
textCheck.Text = "";
} //插入数据SQL 逻辑
string s1 = "insert into Register(account,password,nickname) values ('" + userId.Text + "','" + userPw.Text + "','"
+ nickName.Text + "')"; //编写SQL命令
SqlCommand mycom = new SqlCommand(s1, mycon); //初始化命令
mycom.ExecuteNonQuery(); //执行语句
mycon.Close(); //关闭连接
mycom = null;
mycon.Dispose(); //释放对象 if (userId.Text == "" || userPw.TextLength <= || nickName.Text == "" || ensurePw.Text == ""
|| textCheck.Text == "") {
MessageBox.Show("请将信息填完整");
} else {
MessageBox.Show("注册成功");
this.Close();
}
} private void checkCode_Click(object sender, EventArgs e) {
Random random = new Random();
int minV = , maxV = ;
checkCode.Text = random.Next(minV, maxV).ToString();
}
}
C#基础用户登陆的更多相关文章
- [作业] Python入门基础--用户登陆
让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定登陆 #__author:Mifen #date: 2018/11/28 import time #自定义本地用户名和密码 user_nam ...
- Spring Boot 构建电商基础秒杀项目 (六) 用户登陆
SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...
- JavaWeb基础之Servlet简单实现用户登陆
学习javaweb遇到了一些坑,一些问题总结下来,记个笔记. 学习servlet遇到的一些坑: servlet实现用户登陆遇到的坑解决办法: https://www.cnblogs.com/swxj/ ...
- Android——用户登陆及用户名和密码的保存
Android——用户登陆及用户名和密码的保存 在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...
- 基于gin框架和jwt-go中间件实现小程序用户登陆和token验证
本文核心内容是利用jwt-go中间件来开发golang webapi用户登陆模块的token下发和验证,小程序登陆功能只是一个切入点,这套逻辑同样适用于其他客户端的登陆处理. 小程序登陆逻辑 小程序的 ...
- JMeter (3) —— JMeter录制脚本并压力测试用户登陆场景以CAS SSO为例(101 Tutorial)
JMeter (3) -- JMeter录制脚本并压力测试用户登陆场景以CAS SSO为例(101 Tutorial) 主要内容 JMeter录制脚本并进行压力测试用户登陆场景,并以CAS SSO单点 ...
- JavaWeb 基于Session的用户登陆注销实现
通过Session来存储用户的部分登陆信息来验证用户是否在线,这应该时最容易实现的一种Web端方案,本文以SSM(Spring.SpringMVC.myBatis)框架为载体,来具体实现这套登陆系统. ...
- Django之博客系统:用户登陆
使用django有一个好处就是有各种各样的框架可以拿来直接使用.相比flask,django自带的框架确实要多很多.比如这一章就要介绍的用户登录.Django拥有一个内置的认证(authenticat ...
- linux查看当前用户登陆信息
@(Linux基础)[linux查看当前用户登陆信息] linux查看当前用户登陆信息 作为系统管理员,你可能经常会(在某个时候)需要查看系统中有哪些用户正在活动.有些时候,你甚至需要知道他(她)们正 ...
随机推荐
- linux基础实操四
实操一: 1)为新加的硬盘分区,一个主分区大小为10剩余空间给扩展分区,在扩展分区上划分2个逻辑分别为5G 2)式化主分区为ext3系统 #mkfs.ext3 /dev/sdb1 3 将逻辑分区设置为 ...
- 谷歌、火狐浏览器 缩放为80% 时,margin值才正确
声明:小白的笔记,欢迎大神指点.联系QQ:1522025433. 在网页布局中,通过 谷歌浏览器或火狐浏览器 预览时,发现我们定义的盒模型width,height,margin,padding 值都是 ...
- easyui组件window拖动时超过浏览器顶部则无法拖回
项目研发过程中遇到一个问题,easyui的window可以随意拖动或者放大缩小,但是鼠标只有放在“header”上面时鼠标箭头才会变成四个方向的箭头,也就是只有在这时才能拖动窗口:但是当拖动的窗口超过 ...
- Python内置模块之random
random的方法有 random.random # 返回一个随机的小数 ramdom.uniform # 按照一个区间返回一个小数 random.randint # 返回一个整数 random.ra ...
- 数据库和linux对大小写的区分
- CentOS升级glibc-2.14
升级glibc-2.14用到的rpm https://pan.baidu.com/s/1v-Uk579TGM6498cExst6ow 先要安装gcc yum -y install gcc 执行: rp ...
- curl请求https请求
function curl_https($url,$data){ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_se ...
- HL7体系入门级介绍【转】
HL7的简单介绍1)HL7 缩写于Health Level Seven,是创建于1987年,用来发展独立卫生保健行业的电子交换交换标准,经过多年的发展,HL7已经有多个版本, 目前我们 的集 ...
- 分布式一致性算法——paxos
一.什么是paxos算法 Paxos 算法是分布式一致性算法用来解决一个分布式系统如何就某个值(决议)达成一致的问题. 人们在理解paxos算法是会遇到一些困境,那么接下来,我们带着以下几个问题来学习 ...
- 使用impala操作kudu之创建kudu表(内部表和外部表)
依次启动HDFS.mysql.hive.kudu.impala 登录impala的shell控制端: Impala-shell 1:使用该impala-shell命令启动Impala Shell .默 ...