《2022年源码C#登录注册小程序源码 .pdf》由会员分享,可在线阅读,更多相关《2022年源码C#登录注册小程序源码 .pdf(5页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、C#登录及注册的小程序1) 主界面( Form1 ) :privatevoid linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) this .Hide(); Form3 f3 = new Form3 (); f3.ShowDialog(); / 显示注册界面;privatevoid linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) this .Hide(); Form2 f = new Form2 (); f.S
2、howDialog(); if (f.DialogResult = DialogResult.OK) this .Visible = true ; / 显示登录界面;privatevoid Form1_FormClosing(object sender, FormClosingEventArgs e) try System.Diagnostics.Process myProcesses = System.Diagnostics.Process .GetProcesses(); foreach (System.Diagnostics.Process myProcess in myProcesse
3、s) if (LoginInterface.exe = myProcess.ProcessName) myProcess.Kill(); catch (Exception ee) MessageBox.Show(ee.Message); / 关掉程序;2) 注册界面( Form3 )名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - 本文使用的数据库是sql sever2005,先在引用里加入:using System.Data.S
4、qlClient; 以下为程序代码:publicpartialclass Form3 : Form public Form3() InitializeComponent(); boolflagRegister;/ 定义标志位,确认用户注册string strConnect = Data Source=CAI-PCSQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=*; / 连接数据库字符串privatevoid button1_Click(object sender, EventAr
5、gs e) if (textBox1.Text.Length = 4) & (textBox1.Text.Length = 6) & (textBox3.Text.Length = 6) flagRegister = true ; else if (textBox1.Text.Length 12) MessageBox.Show( 用户名长度不在约定范围内,请重新输入! , 提示 ); return ; if (textBox2.Text.Length 6) MessageBox.Show( 密码长度不足 6位,请重新输入!,提示 ); return ; if (textBox3.Text.L
6、ength 6) MessageBox.Show( 请重新输入邮箱!, 提示 ); return ; / 判断用户名条件;if (UserFlag = true ) MessageBox.Show( 用户已经存在,请重新输入!); return ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - if (flagRegister = true ) / 确认用户注册后,把用户写入数据库 SqlConnection conConne
7、ction = new SqlConnection(strConnect); conConnection.Open(); string cmd = insert into 用户 (用户名 ,密码 ,email) values ( + textBox1.Text + ,+ + textBox2.Text + , + + textBox3.Text + ) ; SqlCommand com = new SqlCommand(cmd, conConnection); com.ExecuteNonQuery(); conConnection.Close(); MessageBox.Show( 注册成功
8、!点击确定,返回登录界面。, 提示 ); this .Close(); Form1 f1 = new Form1 (); f1.label2.Text = 欢迎你, + textBox1.Text; f1.label1.Visible = false ; f1.label3.Visible = false ; f1.linkLabel1.Visible = false ; f1.linkLabel2.Visible = false ; f1.label2.Visible = true ; f1.Show(); publicbool UserFlag; / 定义标志位,来确认用户是否存在priv
9、atevoid textBox1_TextChanged(object sender, EventArgs e) SqlConnection conConnection = new SqlConnection(strConnect); conConnection.Open(); string cmd = select 用户名from 用户 ; SqlCommand com = new SqlCommand(cmd, conConnection); SqlDataReader readerUser = com.ExecuteReader(); while (readerUser.Read() i
10、f (textBox1.Text = readerUser用户名 .ToString().Trim() label5.Text = 用户已存在,请重新输入!; UserFlag = true ; /textBox1.Text = ; return ; else if (textBox1.Text != readerUser用户名 .ToString().Trim() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - label5.
11、Text = 恭喜你,该用户名可以使用。; UserFlag = false ; / 判断用户名是否满足条件privatevoid textBox3_TextChanged(object sender, EventArgs e) int index = textBox3.Text.IndexOf( ); if (index 1) label7.Text = 邮箱格式不正确,请重新输入!; else label7.Text = 邮箱格式正确 ; / 判断邮箱格式是否正确 3) 登录界面( Form2 )本文使用的数据库是sql sever2005,先在引用里加入:using System.Dat
12、a.SqlClient; 以下为程序代码:string User, Pwd; / 用户名,密码bool flagshow = false ;/ 用来标注登录名是否存在于数据库privatevoid linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) this .Hide(); Form3 f3 = new Form3 (); f3.ShowDialog(); / 显示注册界面privatevoid button1_Click(object sender, EventArgs e) / 登录string s
13、trConnect = Data Source=CAI-PCSQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=*; SqlConnection conConnection = new SqlConnection(strConnect); conConnection.Open(); string cmd = select 用户名 ,密码 ,email from 用户 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - -
14、名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - SqlCommand com = new SqlCommand(cmd, conConnection); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()/ 从数据库读取用户信息 User = reader用户名 .ToString(); Pwd = reader密码 .ToString(); if (User.Trim () = textBox1.Text & Pwd.Trim () = textBox2.Tex
15、t) flagshow = true ; / 用户名存在于数据库,则为true reader.Close(); conConnection.Close(); if (flagshow = true ) showMainForm();/ 用户存在,返回登录界面 else MessageBox.Show( 用户不存在或密码错误!, 提示 ); return ; privatevoid showMainForm()/ 登录成功,显示主界面 this .Close(); Form1 f1 = new Form1 (); f1.label1.Visible = false ; f1.label3.Visible = false ; f1.linkLabel1.Visible = false ; f1.linkLabel2.Visible = false ; f1.label2.Visible = true ; f1.label2.Text = 欢迎你, + textBox1 .Text ; f1.Show(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -