二.管理员和普通用户的注册界面
1.管理员注册界面
管理员注册时,如果用一个信息为空,就会提示填写信息不全,无法注册,当都不为空时
如果两次填写的密码不一致也会无法注册,如果注册的信息已存在,同样是无法注册
代码如下
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;
using System.Data.SqlClient;
namespace _112管理系统2._0
{
public partial class Form_adimn : Form
{
public Form_adimn()
{
InitializeComponent();
}
public int t = 0;
public void Panduan()//函数功能是判断注册信息是否已存在
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "")
{
MessageBox.Show("填写的信息不能为空");
}
else
{
//指定Sql Sereve提供者的链接字符串
string ConStr = "server=SC-201910241106;database=我的数据库;Trusted_Connection=SSPI";
//建立链接对象
SqlConnection con = new SqlConnection(ConStr);
//打开连接
con.Open();
string str1 = textBox1.Text;
string str2 = textBox2.Text;
string sql = string.Format("select count(*) from 管理员 where 姓名='{0}'", str1);//数据库查询判断是否已存在
SqlCommand command = new SqlCommand(sql, con);
int i = Convert.ToInt32(command.ExecuteScalar());
if (i > 0)
{
t = 1;
}
else
{
t = 0;
}
con.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text.Trim()==""||textBox2.Text.Trim()==""||textBox3.Text.Trim()=="")
{
MessageBox.Show("注册信息不能为空");
}
else
{
if(textBox2.Text.Trim()!=textBox3.Text.Trim())
{
MessageBox.Show("两次输入的密码不一致");
}
else
{
Panduan();
if(t==0)//如果不存在则插入数据库
{
//指定Sql Sereve提供者的链接字符串
string ConStr = "server=SC-201910241106;database=我的数据库;Trusted_Connection=SSPI";
//建立链接对象
SqlConnection con = new SqlConnection(ConStr);
//打开连接
con.Open();
//把信息插入数据库
SqlCommand com = con.CreateCommand();
string str = "insert into {0}(姓名,密码) values ('{1}','{2}')";
com.CommandText = string.Format(str, "管理员", textBox1.Text.Trim(), textBox2.Text.Trim());
com.ExecuteNonQuery();
MessageBox.Show("注册成功,请前往登录界面");
}
else
{
MessageBox.Show("用户信息已存在,请前往登录界面");
}
}
}
}
private void label5_Click(object sender, EventArgs e)//点击进入登录页面
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
private void Form_adimn_Load(object sender, EventArgs e)
{
}
private void Form_adimn_FormClosed(object sender, FormClosedEventArgs e)//退出程序
{
Application.Exit();
}
}
}
2.普通用户注册界面
用户注册时,如果有一个信息为空,就会提示填写信息不全,无法注册,当都不为空时
如果两次填写的密码不一致也会无法注册,如果注册的信息已存在,同样是无法注册
代码如下
在这里插入代码片`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;
using System.Data.SqlClient;
namespace _112管理系统2._0
{
public partial class Form2 : System.Windows.Forms.Form
{
public Form2()
{
InitializeComponent();
}
public void Insert()//函数功能先判断注册信息是否已存在,在执行插入操作
{
string Constr = "server=SC-201910241106;database=我的数据库;Trusted_Connection=SSPI";
SqlConnection con = new SqlConnection(Constr);
con.Open();//打开链接
//Console.WriteLine("数据库已连接");
//Console.WriteLine("请输入你要插入的学号和姓名");
string str1 = textBox1.Text;
string str2 = textBox2.Text;
string str3 = textBox3.Text;
string str4 = textBox4.Text;
if (str1 == "" || str2 == "" || str3 == "" || str4 == "")
{
MessageBox.Show("信息不能为空");
}
else
{
if (str3 != str4)
{
MessageBox.Show("两次输入的密码不同,请从新输入密码");
}
SqlCommand thiscommand = con.CreateCommand();//为上面的链接指定Command对象
thiscommand.CommandText = "select 学号,姓名 from 学号";
SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader();
int t = 0;
while (thisSqlDataReader.Read())
{
if (thisSqlDataReader["学号"].ToString() == str1)//此学号已经注册
{
t = 1;
break;
}
}
thisSqlDataReader.Close();//关闭流
if (t == 0)//插入数据
{
string sql = "insert into 学号(学号,姓名)values('" + str1 + "','" + str2 + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("学号", str1);
cmd.Parameters.AddWithValue("姓名", str2);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("注册成功");
}
else
{
MessageBox.Show("此信息已存在");
}
}
}
private void button1_Click(object sender, EventArgs e)//注册按钮
{
Insert();
this.Hide();
Form1 form1 = new Form1();
form1.Show();//注册成功返回登录按钮
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)//退出程序
{
Application.Exit();
}
}
}
期待后续更新