登录界面
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 excelToSql
{
public partial class Login_main : Form
{
public Login_main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username, password;
//string username = "";
//string password = "";
username = Username.Text;
password = Password.Text;
//String myconn = "Data Source=(Local);Initial Catalog=Cuser;User ID=sa;Password=tiancaiisme1;";//数据库实例连接字符串
string myconn = "server=localhost; database=MY_DB;integrated security=SSPI";//数据库实例连接字符串
SqlConnection sqlconnection = new SqlConnection(myconn);//新建数据库连接实例
sqlconnection.Open();
//password = Adduser.GetMD5(password); //在同一个命名空间(在同一个文件夹中),可以访问Adduser里的GetMD5函数。 因为MD5加密算法不可逆,所以要把输入的密码加密和数据库里密码匹配。这样做以后,除了用户自己谁也不知道密码了。
string sql = "select username,password from user_info where username='" + username + "' and password='" + password + "'";//SQL语句实现表数据的读取
SqlCommand sqlcommand = new SqlCommand(sql, sqlconnection);
Console.WriteLine(Password);
Console.WriteLine(sql);//控制台输出
SqlDataReader sqlDataReader = sqlcommand.ExecuteReader();
if(Username.Text=="")
{
MessageBox.Show("登录失败请输入用户名");
}
else if (Password.Text=="")
{
MessageBox.Show("登录失败请输入密码");
}
else
{
if (sqlDataReader.HasRows) //满足用户名与密码一致,进入下一个界面
{
//实现页面跳转
Form1 form1 = new Form1();//想要打开的窗体界面
this.Hide();//隐藏当前窗体
form1.ShowDialog();
Application.ExitThread(); //退出当前窗体,这一步很重要,否则最后可能无法将所有进程关闭。最好是在跳转页面后,将之前的页面退出。
}
else//如果登录失败,询问是否注册新用户
{
DialogResult dr = MessageBox.Show("是否注册新用户?", "登录失败", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)//打开注册界面
{
login login1 = new login();
this.Hide();
login1.ShowDialog();
Application.ExitThread();
}
else
{
Username.Text = "";
Password.Text = "";
Username.Focus();
this.Show();
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
login login1 = new login();//单击注册按钮
this.Hide();
login1.ShowDialog();
Application.ExitThread();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
Password.PasswordChar = '\0'; //显示输入
}
else
{
Password.PasswordChar = '*';//显示*
}
}
private void Login_main_Load(object sender, EventArgs e)
{
Password.PasswordChar = '*'; //设置文本框的PasswordChar属性为字符@
//Password.UseSystemPasswordChar = true;
//设置文本框的UseSystemPasswordChar属性为True;
}
}
}
注册界面
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 excelToSql
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string username, password, repassword;
username = Username.Text;
password = Password.Text;
repassword = Repassword.Text;
if(Username.Text=="")
{
MessageBox.Show("登陆失败,请输入用户名");
Username.Focus();
}
else if(Password.Text=="")
{
MessageBox.Show("登陆失败,请输入密码");
Password.Focus();
}
else if (Repassword.Text == "")
{
MessageBox.Show("密码确认错误,请重新输入");
Repassword.Focus();
}
else
{
//String myConn = "Data Source=(Local);Initial Catalog=Cuser;User ID=sa;Password=tiancaiisme1;";
string myconn1 = "server=localhost; database=MY_DB;integrated security=SSPI";//数据库实例连接字符串
SqlConnection sqlConnection = new SqlConnection(myconn1); //实例化连接对象
sqlConnection.Open();
String sql = "select userName from user_info where username='" + username + "'";//SQL语句实现表数据的读取
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
sqlConnection.Close();
MessageBox.Show("该用户名已存在,请重新注册", "注册失败");
Username.Text = "";
Password.Text = "";
Repassword.Text = "";
Username.Focus(); //指定光标在哪个textBox处闪烁
}
else
{
if (password == repassword)//两次输入的密码一致
{
sqlConnection.Close();
//string myConn2 = "Data Source=(Local);Initial Catalog=Cuser;User ID=sa;Password=tiancaiisme1;";
string myconn2 = "server=localhost; database=MY_DB;integrated security=SSPI";//数据库实例连接字符串
SqlConnection sqlConnection2 = new SqlConnection(myconn2); //实例化连接对象
sqlConnection.Open();
//password = GetMD5(password);
String sql2 = "INSERT INTO user_info(username,password) VALUES('" + username + "','" + password + "')";//SQL语句向表中写入数据
SqlCommand sqlCommand2 = new SqlCommand(sql2, sqlConnection);
sqlCommand2.ExecuteNonQuery();
sqlConnection2.Close();
DialogResult dr = MessageBox.Show("是否返回主界面", "注册成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)//打开注册界面
{
login form2 = new login();
this.Hide();
form2.ShowDialog();
Application.ExitThread();
}
else
{
Username.Text = "";
Password.Text = "";
Repassword.Text = "";
this.Show();
}
}
else
{
MessageBox.Show("两次输入密码不一致", "错误信息");
Username.Text = "";
Password.Text = "";
Repassword.Text = "";
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Login_main main = new Login_main();
this.Hide();
main.ShowDialog();
Application.ExitThread();
}
}
}