【数据库课程设计】用户登录(一)

学生成绩管理系统

背景资料:

1)   一个学校有不同专业,不同年级的若干学生。2)   每个专业有不同的课程,每个学生选修不同的课程,每门课程被多个学生选修。3)   每个学生选修某门课程得到一个成绩。

设计要求:

4)  进行需求分析,编写数据字典。5)        设计E-R图。

1) 实现学生基本情况的录入、修改、删除等基本操作。对学生基本信息提供灵活的查询方式。2)  实现学生成绩的录入、修改、删除等基本操作。

3) 能方便的对学生成绩进行查询。4)  实现分类查询。5)  能够删除毕业学生的数据。6) 要有用户登陆和身份验证部分;

首先创建一个数据库StudentManage.mdf,新建第一个表:StudentAccount,其中ID设为主键+标识(Name用户名,Password密码,UserType账号类型)

我们刚开始先弄button,label,textbox,就这样排版,属性可以修改显示名称

右键查看代码,或者双击控件也能进去

连接数据库,这里工具里面直接搞

 

进入App.config设置一下数据库吧

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <connectionStrings>
    <add name="SQL"
        connectionString="Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=E:codeTEXTSQLStudentManage.mdf;Integrated Security=True;Connect Timeout=30"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

回到Form代码界面,准备一些引用,

没学过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.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ClassHomeWork
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string UserName;
        public static string UserType;
        public static int ID;
        string connstr = ConfigurationManager.ConnectionStrings["StudentSql"].ConnectionString;
        //定义全局变量 获取类型+学号+id
        // 链接数据库,命名为connstr
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connstr);
            //一个数据库到conn的链接
            string sql = "select Password,UserType,ID from StudentAcconut where Name='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();//打开连接
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                string pwd = reader.GetString(0).Trim();
                string utype = reader.GetString(1);
                int id = reader.GetInt32(2);
                if (pwd == textBox2.Text)
                {
                    UserName = textBox1.Text;
                    UserType = utype;
                    ID = id;
                    MessageBox.Show("系统登录成功,正在跳转主页面...");
                    // MainForm mainForm = new MainForm();
                    // mainForm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("密码错误!请再次输入!");
                    textBox2.Text = "";
                }
            }
            else
            {
                MessageBox.Show("用户名不存在,请重新输入!");
                textBox1.Text = "";
            }
        }
    }
}

运行结果(第一版没什么好说的)

哦哦,密码是不能给人家看的,属性设置一下吧

原文地址:https://www.cnblogs.com/yinghualuowu/p/6225650.html