【数据库实验2】VS——C#登录

 


 

  任务:用C#制作一个登录登录界面

 

目录

1.新建窗体,设计

2.添加新窗体

3.写代码:取出账号、密码;密码加密

4. ※※※连接数据库※※※

5.用C#写SQL语句

6.新建窗体,显示数据库中的表

7.写代码:关闭、打开等操作

8.写代码:登录成功、失败

 

界面展示

演示

代码展示

 

总结

 

插眼:Trim()函数

插眼:修改sa密码(先挖个坑,之后填上)

插眼:(吐槽)这里真的出了好多好多好多好多好多问题...

 

7.写代码:关闭、打开等操作(显示、隐藏、跳转)

 


 

1.新建窗体,设计

 

  如图,分别为LabelTextBoxButton

 

 


 

2.添加新窗体

 

 

 

 

 


 

3.写代码:取出账号、密码;密码加密

 

string username = textBox1.Text.Trim();    //取出账号
string password = textBox2.Text.Trim();    //取出密码

 

插眼:Trim()函数

  是取出字符串中的空格,因为账号、密码不会出现空格。

 

  因为密码需要加密、无法显示,将密码处的TextBox的PasswordChar用来代替密码的字符。

 

 


 

4. ※※※连接数据库※※※

 

  SQL中的sa为系统管理员,点击即可查看、修改本机的密码。

 

插眼:修改sa密码(先挖个坑,之后填上)

 

 

 

  C#连接SQL,(代码中的***即为密码,此处加密)

string myConnString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=*******;

SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接  

sqlConnection.Open();

 

  .是本机、STU是要连接的数据库、sa是用户名。

 

  注:

  这里会报错,双击点击即可消除错误。

 

 


 

5.用C#写SQL语句

 

  将用户账号、密码存入Usertable表中,将表中的账号、密码与username和password进行对比。

 

string sql = "select Userid,Password from Usertable where Userid = '" 
        + username + "' and Password = '" + password + "'";        //编写SQL命令

SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);        //在数据库上执行SELECT操作 

SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();      //读取

 

  Usertable表中的账号、密码。

 

 

插眼:(吐槽)这里真的出了好多好多好多好多好多问题...

 

 

6.新建窗体,显示数据库中的表

 

  点击DataGridView

 

 

  点击右上角的三角,点击添加项目数据源,选择数据库——→数据集——→新建连接——→选择Microsoft SQL Server。

  服务器名:.;

  身份验证选择SQL Server验证;

  用户名、密码即为上文提到的管理员sa及密码。

  选择要用到的数据库:STU。

 

 

 

 

 

 

 

 

 

 

 

 

  注:勾选“显示将保存在应用程序中的连接字符串”。这段即为前文提到的连接处的代码。

  之后接点击下一步。

  最后选择用到的对象,这里选择数据库中的

 

 

 

 

 

 

 


 

7.写代码:关闭、打开等操作

 

  实例化窗体。

 

Form4 form4 = new Form4();       //实例化

 

  显示、隐藏、关闭窗体。

 

form2.Show();                    //显示窗体
this.Hide();                     //隐藏当前窗体
this.Close();                   //关闭当前窗体

 

  跳转至另一窗体。

 

form4.ShowDialog();

 

  结束程序。

 

Application.Exit();

 


 

8.写代码:登录成功、失败

 

  

if (sqlDataReader.HasRows)       //登录成功
{           
    form2.Show();
}

    else         //登录失败
    {
        form3.Show();
    }

 

  注:这里用的方法,若登录成功打开form2,在form2的代码中,点击form2的确定跳转至form4.

 


 

界面展示

 

  form1——主界面:

 

 

  form2——登录成功:

 

 

  form3——登录失败:

 

 

  form4——数据库表界面:

 

 


 

演示

 

  登录失败:

 

 

  登录成功:

 

 

  点击确定跳转至form4——表界面。

 

 

  点击取消结束所有程序。

 


 

代码展示

 

  form1——主界面:

  注:这里的Password我用***代替了。

 

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.Tasks;
using System.Windows.Forms;

namespace Login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)      //“确定”按钮
        {
            string username = textBox1.Text.Trim();     //取出账号
            string password = textBox2.Text.Trim();     //取出密码


            string myConnString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******";      //连接数据库
            SqlConnection sqlConnection = new SqlConnection(myConnString);      //实例化连接对象
            sqlConnection.Open();

            string sql = "select Userid,Password from Usertable where Userid = '" + username + "' and Password = '" + password + "'";     //编写SQL命令

            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);     //在数据库上执行SELECT操作 


            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();               //读取数据


            Form4 form4 = new Form4();       //实例化
            Form3 form3 = new Form3();       //实例化
            Form2 form2 = new Form2();       //实例化



            if (sqlDataReader.HasRows)       //登录成功
            {           
                this.Hide();                     //隐藏当前窗体
                form2.ShowDialog();             //新打开窗体
                this.Close();                   //关闭当前窗体
            }

            else         //登录失败
            {
                form3.Show();
            }

            sqlConnection.Close();

        }


        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


    }
}

 

  form2——登录成功:

 

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;

namespace Login
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();                     //隐藏当前窗体
            Form4 form4 = new Form4();       //实例化
            form4.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }
    }
}

 

  form3——登录失败:

 

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;

namespace Login
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

 

  form4——数据库表界面:

 

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;

namespace Login
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }



        private void Form4_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“sTUDataSet3.Student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter.Fill(this.sTUDataSet3.Student);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

 


 

总结

 

  本来以为很简单,真的出了好多好多问题...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章