一、建立数据库及相关表
二、搭建两个窗体
登录窗体:
三、添加一个管理员类
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp3
{
public class SystemAdministrator
{
/// <summary>
/// 编号
/// </summary>
private int id;
/// <summary>
/// 账号
/// </summary>
private string account;
/// <summary>
/// 密码
/// </summary>
private string password;
public int Id { get => id; set => id = value; }
public string Account { get => account; set => account = value; }
public string Password { get => password; set => password = value; }
}
}
四、添加一个DBHelper类
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApp3
{
class DBHelper
{
public static string connString = "server=.;database=Today;integrated security=true";
public static SqlConnection conn = new SqlConnection(connString);
//增删改
public static bool ExecuteNonQuery(string sql)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
int result=cmd.ExecuteNonQuery();
conn.Close();
return result > 0;
}
//查询
public static DataTable GetDataTable(string sql)
{
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter dap = new SqlDataAdapter(sql,conn);
dap.Fill(dt);
conn.Close();
return dt;
}
/// <summary>
/// 查询学生信息
/// </summary>
/// <param name="studentName"></param>
/// <returns></returns>
public DataTable GetStudentTable(string studentName)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(String.Format("select StudentName as 姓名,Age as 年龄,StudentClass as 班级 from StudentInfo where StudentName like '%{0}%'",studentName),connString);
sda.Fill(dt);
sda.Dispose();
return dt;
}
/// <summary>
/// 获取学生信息
/// </summary>
/// <returns></returns>
public DataTable GetStudentTable()
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select StudentName as 姓名,Age as 年龄,StudentClass as 班级 from StudentInfo",connString);
sda.Fill(dt);
sda.Dispose();
return dt;
}
/// <summary>
/// 判断用户是否存在
/// </summary>
/// <param name="account"></param>
/// <param name="password"></param>
/// <returns></returns>
public SystemAdministrator GetAdmin(string account,string password)
{
SystemAdministrator sa = null;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(String.Format("select * from SystemAdministrator where account='{0}' and pwd='{1}'",account,password),connString);
sda.Fill(dt);
sda.Dispose();
if (dt.Rows.Count > 0)
{
sa = new SystemAdministrator();
sa.Account = dt.Rows[0]["Account"].ToString();
sa.Password = dt.Rows[0]["Pwd"].ToString();
}
return sa;
}
}
}
五、窗体Login的代码
代码:
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 WindowsFormsApp3
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string account = this.txtAccount.Text;
string password = this.txtPwd.Text;
DBHelper db = new DBHelper();
SystemAdministrator sa = db.GetAdmin(account, password);
if (sa != null)
{
//管理员存在,弹出学生信息窗口
StudentManager sm = new StudentManager(sa.Account);
sm.Show();
}
else
{
MessageBox.Show("用户名或密码不正确");
}
}
}
}
窗体StudentManager的代码:
代码:
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 WindowsFormsApp3
{
public partial class StudentManager : Form
{
DBHelper db = null;
public StudentManager(string account)
{
InitializeComponent();
db = new DBHelper();
label2.Text = account;
dataGridView1.DataSource = db.GetStudentTable();
}
private void button1_Click(object sender, EventArgs e)
{
string studentName = this.textBox1.Text;
if (!String.IsNullOrEmpty(studentName))
{
dataGridView1.DataSource = db.GetStudentTable(studentName);
}
else
{
dataGridView1.DataSource = db.GetStudentTable();
}
}
}
}