4个Form分别是:登录form、管理员form、教师form、学生form。
Data类主要用来保存一些各个Form之间通用的string、函数,还可以用来保存数据,在Form之间传递参数。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SSMS
{
class Data
{
public static int classCount;
public static bool ok = false;
public static string[] s = new string[100];
public static string connectionString = "server=WB-PC\\testDB;database=ASchool;Integrated Security=True";
public static string loginID;
//Login表的表名,列名
public static string t_login = "Login";
public static string c_login_username = "userID";
public static string c_login_password = "password";
public static string c_login_role = "role";
//SC表的表名,列名
public static string t_sc = "SC";
public static string c_sc_snum = "snum";
public static string c_sc_cnum = "cnum";
public static string c_sc_score = "score";
//TableCourse表的表名,列名
public static string t_course = "TableCourse";
public static string c_course_cnum = "cnum";
public static string c_course_cname = "cname";
public static string c_course_tnum = "tnum";
//TableStudent表的表名,列名
public static string t_student = "TableStudent";
public static string c_student_snum = "snum";
public static string c_student_sname = "sname";
public static string c_student_sclass = "sclass";
//TableTeacher表的表名,列名
public static string t_teacher = "TableTeacher";
public static string c_teacher_tnum = "tnum";
public static string c_teacher_tname = "tname";
public static string c_teacher_cname = "cname";
public static string c_teacher_cnum = "cnum";
//Enroll表的表名,列名 选课开关
public static string t_enroll = "Enroll";
public static string c_enroll_switch = "switch";
//两个视图的名称
public static string v_score = "S_C_Score_View";
public static string v_class = "Class_View";
//DeleteStudent存储过程 名称、参数
public static string sp_delStudent = "spDeleteStudent";
public static string sp_delStudent_para = "@snum";
//DeleteTeacher存储过程 名称、参数
public static string sp_delTeacher = "spDeleteTeacher";
public static string sp_delTeacher_para = "@tnum";
///
/// 自动调整列宽,以适应表格大小
///
/// 需要被调整的DataGridView对象
public static void columnAutoAdapter(DataGridView dv)
{
int totalWidth = 0;
for (int i = 0; i < dv.Columns.Count; i++)
{
totalWidth += dv.Columns[i].Width;//记录整个DataGridView的宽度
}
for (int i = 0; i < dv.Columns.Count; i++)
{
dv.Columns[i].FillWeight = (dv.Columns[i].Width * 100) / totalWidth;
}
dv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
///
/// 获取班级总个数
///
/// 班级个数
public static int getClassCount()
{
return classCount;
}
///
/// 获取所有班级名称
///
/// 班级名称数组
public static string[] getAllClass()
{
if (ok)
{
return s;
}
SqlConnection con = new SqlConnection(Data.connectionString);
con.Open();
//.........
string sqlStr = @"select * from {0}";
sqlStr = string.Format(sqlStr, Data.v_class);
SqlCommand cmd = new SqlCommand(sqlStr, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
int i = 0;
while (dr.Read())
{
if (i < 100)
{
s[i++] = dr[Data.c_student_sclass].ToString();
}
}
classCount = i;
}
con.Close();
ok = true;
return s;
}
///
/// 根据查询字符串sqlStr,将查询结果填充到数据表dt中
///
/// SQL查询语句串
/// 存放查询结果的表格
public static void fillDataTable(string sqlStr, DataTable dt)
{
dt.Clear();
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, Data.connectionString);
sda.Fill(dt);
}
///
/// 从source表格中根据查询的关键字,筛选出符合条件的行,并保存到target表格中。
/// 多字段查询----数字项精确查询,字符串类型模糊查询。
///
///
/// 源表格
/// 存储筛选结果的表格
/// 关键字: 默认值为"", 表示无任何操作
/// int类型的列名数组: 默认为null
/// string类型的列名数组: 默认为null
///
/// void
public static void filterDataRows(DataTable source, DataTable target, string[] colsLike = null, string[] colsIntEqual = null, string filterValue = "")
{
if (filterValue == "")
{
return;
}
target.Clear();
string filterString = "";
if (colsLike != null)
{
for (int i = 0; i < colsLike.Length; ++i)
{
filterString += (colsLike[i] + " like '%" + filterValue + "%' ");
if (i != (colsLike.Length - 1))
{
filterString += " or ";
}
}
}
if (colsIntEqual != null)
{
if (colsLike != null)
{
filterString += " or ";
}
int myId;
try
{
myId = int.Parse(filterValue);
}
catch(Exception){
myId = -1;
}
for (int i = 0; i < colsIntEqual.Length; ++i)
{
filterString += (colsIntEqual[i] + " = '" + myId + "' ");
if (i != (colsIntEqual.Length - 1))
{
filterString += " or ";
}
}
}
DataRow[] rows = source.Select(filterString);
if (rows == null || rows.Length == 0)
{
return;
}
foreach (DataRow row in rows)
{
target.ImportRow(row);
}
}
}
}
connectionString:用来连接数据库的字符串,server的值是你当前SQL Server登录时显示服务器名,跟之前数据导入时填的数据库名一致,database即创建的数据库的名字,Integrated Security=True表示用Windows登录验证方式,这个要在安装SQL Server是设置成混合验证方式访问。
loginID:将登录用户的ID暂存于此,便于以后调用。
public static void fillDataTable(string sqlStr, DataTable dt)
fillDataTable: 根据查询语句sqlStr将查询结果填充到DataTable中。
public static void filterDataRows(DataTable source, DataTable target, string[] colsLike = null, string[] colsIntEqual = null, string filterValue = "")
filterDataRows: 在本地数据副本source表格中根据 filterValue 查询符合条件的记录, 将查询结果填充到 target表格中。这样提高了查询效率,减少了访问服务器的次数, 减轻了服务器的压力。
其他的string都是表名、列名,这样做是为了利用VS的自动提示功能加快编码的速度,再者减少出错的机会,最大的好处就是大大增强了程序的灵活性,如果程序中有100处用到了SC这个表名,但是由于某些原因,不得不将表名改为SC2,那么只需要修改Data中的这一个string就解决问题了,是不是很灵活呢?
这种设计还体现了一种原则,Don’t Repeat Yourself,将相同功能的代码抽取出来放到一个函数里,直接调用即可,后面会看到这些函数的调用。
源代码文件下载地址