C#窗体实现登陆注册

在展示内容前,先说明一下遇到的几个小问题:
1.在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)。 解决方法: 启动这三个服务。
C#窗体实现登陆注册
文章图片

2.System.Data.SqlClient.SqlException: 用户xxxx 登录失败: 解决方法: 观察连接字符串中是否有这一句:

Integrated Security=True;

当为false时,将在连接中指定用户ID和密码。当为true时,将使用当前的Windows帐户凭据进行身份验证。
参考博客:https://blog.csdn.net/langwen2048/article/details/80116665?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.add_param_isCf 简化了大佬的做法,自己做的还是比较菜。数据库用的sql server。
登陆界面: C#窗体实现登陆注册
文章图片

using System; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class login : Form { public login() { InitializeComponent(); }private void button1_Click(object sender, EventArgs e)//登陆按钮 {String username, password; username = UserName.Text; password = Password.Text; String myconn = "Data Source=(Local); Initial Catalog=Cuser; User ID=sa; Password=tiancaiisme1; "; //数据库实例连接字符串 SqlConnection sqlConnection = new SqlConnection(myconn); //新建数据库连接实例 sqlConnection.Open(); //打开数据库连接 String sql = "select UserName,Password from users 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("请输入用户名", "登陆失败"); UserName.Focus(); } else if (Password.Text == "") { MessageBox.Show("请输入密码", "登陆失败"); } else { if (sqlDataReader.HasRows)//满足用户名与密码一致,进入下一个界面 { //实现页面跳转 Form1 form3 = new Form1(); this.Hide(); //隐藏当前窗体 form3.ShowDialog(); Application.ExitThread(); //退出当前窗体,这一步很重要,否则最后可能无法将所有进程关闭。最好是在跳转页面后,将之前的页面退出。 } else//如果登录失败,询问是否注册新用户 { DialogResult dr = MessageBox.Show("是否注册新用户?", "登录失败", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes)//打开注册界面 {register form2 = new register(); this.Hide(); form2.ShowDialog(); Application.ExitThread(); } else { UserName.Text = ""; Password.Text = ""; UserName.Focus(); this.Show(); } } } sqlConnection.Close(); }private void button2_Click(object sender, EventArgs e)//单击注册按钮 {register form2 = new register(); this.Hide(); form2.ShowDialog(); Application.ExitThread(); }private void Quit_Click(object sender, EventArgs e)//单击退出按钮 { Application.Exit(); } } }

注册界面: 【C#窗体实现登陆注册】C#窗体实现登陆注册
文章图片

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.Data.SqlClient; using System.Windows.Forms; using System.Security.Cryptography; namespace WindowsFormsApp1 { public partial class register : Form { public register() { InitializeComponent(); }private void button1_Click(object sender, EventArgs e)//注册按钮 { String username, password, repassword; username = textBox1.Text; password = textBox2.Text; repassword = textBox3.Text; if (textBox1.Text == "") { MessageBox.Show("请输入用户名", "注册失败"); textBox1.Focus(); } else if (textBox2.Text == "") { MessageBox.Show("请输入密码", "注册失败"); textBox2.Focus(); } else if (textBox3.Text == "") MessageBox.Show("请确认密码", "注册失败"); else { String myConn = "Data Source=(Local); Initial Catalog=Cuser; User ID=sa; Password=tiancaiisme1; "; SqlConnection sqlConnection = new SqlConnection(myConn); //实例化连接对象 sqlConnection.Open(); String sql = "select UserName from users where UserName='" + username + "'"; //SQL语句实现表数据的读取 SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { sqlConnection.Close(); MessageBox.Show("该用户名已存在,请重新注册", "注册失败"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox1.Focus(); //指定光标在哪个textBox处闪烁 } else { if (password == repassword)//两次输入的密码一致 { sqlConnection.Close(); string myConn2 = "Data Source=(Local); Initial Catalog=Cuser; User ID=sa; Password=tiancaiisme1; "; SqlConnection sqlConnection2 = new SqlConnection(myConn2); //实例化连接对象 sqlConnection.Open(); //password = GetMD5(password); String sql2 = "INSERT INTO users(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 { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; this.Show(); }} else { MessageBox.Show("两次输入密码不一致", "错误信息"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } } }} private void button2_Click(object sender, EventArgs e) //返回登陆按钮 { login form3 = new login(); this.Hide(); form3.ShowDialog(); Application.ExitThread(); }} }

    推荐阅读