在开发应用程序时,可以将数据库的相关操作以及对一些控件的设置封装在单独的类中,便于重复调用,一来提高程序的复用性,二来减少代码量;
我们在DataClass和ModulClass文件夹下添加类,分别为MyMeans类和MyModule类。
下面主要来讲解这两个公共类。
该类主要封装了所有与数据库链接的方法,可以通过该类进行数据库的连接以及对信息进行添加、删除、修改以及读取等操作。
主要代码以及注释如下:
MyMeans.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace PWMS.DataClass
{
class MyMeans
{
#region 全局变量
public static string Login_ID = ""; //定义全局变量,记录当前登录的用户编号
public static string Login_Name = ""; //定义全局变量,记录当前登录的用户名
public static string Mean_SQL = "", Mean_Table = "", Mean_Field = ""; //定义全局变量,记录“基础信息”各窗体中的表名及SQL语句
public static SqlConnection My_con; //定义一个SqlConnection类型的公共变量My_con,用于判断数据库是否连接成功
public static string M_str_sqlcon = "Data Source=MRKJ_ZHD\\EAST;Database=db_PWMS;User id=sa;PWD=111";
//public static string M_str_sqlcon = @"Data Source=LVSHUANG0927\\HCDY;Initial Catalog=db_PWMS;Integrated Security=True";
public static int Login_n = 0; //用户登录与重新登录的标识
public static string AllSql = "Select * from tb_Stuffbusic"; //存储职工基本信息表中的SQL语句
//public static int res = 0;
#endregion
#region 建立数据库连接
///
/// 建立数据库连接.
///
/// 返回SqlConnection对象
public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon); //用SqlConnection对象与指定的数据库相连接
My_con.Open(); //打开数据库连接
return My_con; //返回SqlConnection对象的信息
}
#endregion
#region 测试数据库是否赋加
///
/// 测试数据库是否赋加
///
public void con_open()
{
getcon();
//con_close();
}
#endregion
#region 关闭数据库连接
///
/// 关闭于数据库的连接.
///
public void con_close()
{
if (My_con.State == ConnectionState.Open) //判断是否打开与数据库的连接
{
My_con.Close(); //关闭数据库的连接
My_con.Dispose(); //释放My_con变量的所有空间
}
}
#endregion
#region 读取指定表中的信息
///
/// 读取指定表中的信息.
///
///SQL语句 /// 返回bool型
public SqlDataReader getcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand My_com = My_con.CreateCommand(); //创建一个SqlCommand对象,用于执行SQL语句
My_com.CommandText = SQLstr; //获取指定的SQL语句
SqlDataReader My_read = My_com.ExecuteReader(); //执行SQL语名句,生成一个SqlDataReader对象
return My_read;
}
#endregion
#region 执行SqlCommand命令
///
/// 执行SqlCommand
///
///SQL语句 public void getsqlcom(string SQLstr)
{
getcon(); //打开与数据库的连接
SqlCommand SQLcom = new SqlCommand(SQLstr, My_con); //创建一个SqlCommand对象,用于执行SQL语句
SQLcom.ExecuteNonQuery(); //执行SQL语句
SQLcom.Dispose(); //释放所有空间
con_close(); //调用con_close()方法,关闭与数据库的连接
}
#endregion
#region 创建DataSet对象
///
/// 创建一个DataSet对象
///
///SQL语句 ///表名 /// 返回DataSet对象
public DataSet getDataSet(string SQLstr, string tableName)
{
getcon(); //打开与数据库的连接
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); //创建一个SqlDataAdapter对象,并获取指定数据表的信息
DataSet My_DataSet = new DataSet(); //创建DataSet对象
SQLda.Fill(My_DataSet, tableName); //通过SqlDataAdapter对象的Fill()方法,将数据表信息添加到DataSet对象中
con_close(); //关闭数据库的连接
return My_DataSet; //返回DataSet对象的信息
//WritePrivateProfileString(string section, string key, string val, string filePath);
}
#endregion
}
}
该类将系统中所有窗体的动态调用以及动态生成添加、修改、删除和查询的SQl语句等全部封装到指定的自定义方法中,以便在开发程序时进行重复调用,这样可以大大简化程序的开发过程。主要代码如下:
MyMoodule.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace PWMS.ModuleClass
{
class MyModule
{
#region 公共变量
DataClass.MyMeans MyDataClass = new PWMS.DataClass.MyMeans(); //声明MyMeans类的一个对象,以调用其方法
public static string ADDs = ""; //用来存储添加或修改的SQL语句
public static string FindValue = ""; //存储查询条件
public static string Address_ID = ""; //存储通讯录添加修改时的ID编号
public static string User_ID = ""; //存储用户的ID编号
public static string User_Name = ""; //存储用户名
#endregion
#region 窗体的调用
///
/// 窗体的调用.
///
///调用窗体的Text属性值 ///标识 public void Show_Form(string FrmName, int n)
{
if (n == 1)
{
if (FrmName == "人事档案管理") //判断当前要打开的窗体
{
PerForm.F_ManFile FrmManFile = new PWMS.PerForm.F_ManFile();
FrmManFile.Text = "人事档案管理"; //设置窗体名称
FrmManFile.ShowDialog(); //显示窗体
FrmManFile.Dispose();
}
if (FrmName == "人事资料查询")
{
PerForm.F_Find FrmFind = new PWMS.PerForm.F_Find();
FrmFind.Text = "人事资料查询";
FrmFind.ShowDialog();
FrmFind.Dispose();
}
if (FrmName == "人事资料统计")
{
PerForm.F_Stat FrmStat = new PWMS.PerForm.F_Stat();
FrmStat.Text = "人事资料统计";
FrmStat.ShowDialog();
FrmStat.Dispose();
}
if (FrmName == "员工生日提示")
{
InfoAddForm.F_ClewSet FrmClewSet = new PWMS.InfoAddForm.F_ClewSet();
FrmClewSet.Text = "员工生日提示"; //设置窗体名称
FrmClewSet.Tag = 1; //设置窗体的Tag属性,用于在打开窗体时判断窗体的显示类形
FrmClewSet.ShowDialog(); //显示窗体
FrmClewSet.Dispose();
}
if (FrmName == "员工合同提示")
{
InfoAddForm.F_ClewSet FrmClewSet = new PWMS.InfoAddForm.F_ClewSet();
FrmClewSet.Text = "员工合同提示";
FrmClewSet.Tag = 2;
FrmClewSet.ShowDialog();
FrmClewSet.Dispose();
}
if (FrmName == "日常记事")
{
PerForm.F_WordPad FrmWordPad = new PWMS.PerForm.F_WordPad();
FrmWordPad.Text = "日常记事";
FrmWordPad.ShowDialog();
FrmWordPad.Dispose();
}
if (FrmName == "通讯录")
{
PerForm.F_AddressList FrmAddressList = new PWMS.PerForm.F_AddressList();
FrmAddressList.Text = "通讯录";
FrmAddressList.ShowDialog();
FrmAddressList.Dispose();
}
if (FrmName == "备份/还原数据库")
{
PerForm.F_HaveBack FrmHaveBack = new PWMS.PerForm.F_HaveBack();
FrmHaveBack.Text = "备份/还原数据库";
FrmHaveBack.ShowDialog();
FrmHaveBack.Dispose();
}
if (FrmName == "清空数据库")
{
PerForm.F_ClearData FrmClearData = new PWMS.PerForm.F_ClearData();
FrmClearData.Text = "清空数据库";
FrmClearData.ShowDialog();
FrmClearData.Dispose();
}
if (FrmName == "重新登录")
{
F_Login FrmLogin = new F_Login();
FrmLogin.Tag = 2;
FrmLogin.ShowDialog();
FrmLogin.Dispose();
}
if (FrmName == "用户设置")
{
PerForm.F_User FrmUser = new PWMS.PerForm.F_User();
FrmUser.Text = "用户设置";
FrmUser.ShowDialog();
FrmUser.Dispose();
}
if (FrmName == "计算器")
{
System.Diagnostics.Process.Start("calc.exe");
}
if (FrmName == "记事本")
{
System.Diagnostics.Process.Start("notepad.exe");
}
if (FrmName == "系统帮助")
{
System.Diagnostics.Process.Start("readme.doc");
}
}
if (n == 2)
{
String FrmStr = ""; //记录窗体名称
if (FrmName == "民族类别设置") //判断要打开的窗体
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Folk"; //SQL语句
DataClass.MyMeans.Mean_Table = "tb_Folk"; //表名
DataClass.MyMeans.Mean_Field = "FolkName"; //添加、修改数据的字段名
FrmStr = FrmName;
}
if (FrmName == "职工类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_EmployeeGenre";
DataClass.MyMeans.Mean_Table = "tb_EmployeeGenre";
DataClass.MyMeans.Mean_Field = "EmployeeName";
FrmStr = FrmName;
}
if (FrmName == "文化程度设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Kultur";
DataClass.MyMeans.Mean_Table = "tb_Kultur";
DataClass.MyMeans.Mean_Field = "KulturName";
FrmStr = FrmName;
}
if (FrmName == "政治面貌设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Visage";
DataClass.MyMeans.Mean_Table = "tb_Visage";
DataClass.MyMeans.Mean_Field = "VisageName";
FrmStr = FrmName;
}
if (FrmName == "部门类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Branch";
DataClass.MyMeans.Mean_Table = "tb_Branch";
DataClass.MyMeans.Mean_Field = "BranchName";
FrmStr = FrmName;
}
if (FrmName == "工资类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Laborage";
DataClass.MyMeans.Mean_Table = "tb_Laborage";
DataClass.MyMeans.Mean_Field = "LaborageName";
FrmStr = FrmName;
}
if (FrmName == "职务类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Business";
DataClass.MyMeans.Mean_Table = "tb_Business";
DataClass.MyMeans.Mean_Field = "BusinessName";
FrmStr = FrmName;
}
if (FrmName == "职称类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_Duthcall";
DataClass.MyMeans.Mean_Table = "tb_Duthcall";
DataClass.MyMeans.Mean_Field = "DuthcallName";
FrmStr = FrmName;
}
if (FrmName == "奖惩类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_RPKind";
DataClass.MyMeans.Mean_Table = "tb_RPKind";
DataClass.MyMeans.Mean_Field = "RPKind";
FrmStr = FrmName;
}
if (FrmName == "记事本类别设置")
{
DataClass.MyMeans.Mean_SQL = "select * from tb_WordPad";
DataClass.MyMeans.Mean_Table = "tb_WordPad";
DataClass.MyMeans.Mean_Field = "WordPad";
FrmStr = FrmName;
}
InfoAddForm.F_Basic FrmBasic = new PWMS.InfoAddForm.F_Basic();
FrmBasic.Text = FrmStr; //设置窗体名称
FrmBasic.ShowDialog(); //显示调用的窗体
FrmBasic.Dispose();
}
}
#endregion
#region 将StatusStrip控件中的信息添加到treeView控件中
///
/// 读取菜单中的信息.
///
///TreeView控件 ///MenuStrip控件 public void GetMenu(TreeView treeV, MenuStrip MenuS)
{
for (int i = 0; i < MenuS.Items.Count; i++) //遍历MenuStrip组件中的一级菜单项
{
//将一级菜单项的名称添加到TreeView组件的根节点中,并设置当前节点的子节点newNode1
TreeNode newNode1 = treeV.Nodes.Add(MenuS.Items[i].Text);
//将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i];
//判断当前菜单项中是否有二级菜单项
if (newmenu.HasDropDownItems && newmenu.DropDownItems.Count > 0)
for (int j = 0; j < newmenu.DropDownItems.Count; j++) //遍历二级菜单项
{
//将二级菜单名称添加到TreeView组件的子节点newNode1中,并设置当前节点的子节点newNode2
TreeNode newNode2 = newNode1.Nodes.Add(newmenu.DropDownItems[j].Text);
//将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
ToolStripDropDownItem newmenu2 = (ToolStripDropDownItem)newmenu.DropDownItems[j];
//判断二级菜单项中是否有三级菜单项
if (newmenu2.HasDropDownItems && newmenu2.DropDownItems.Count > 0)
for (int p = 0; p < newmenu2.DropDownItems.Count; p++) //遍历三级菜单项
//将三级菜单名称添加到TreeView组件的子节点newNode2中
newNode2.Nodes.Add(newmenu2.DropDownItems[p].Text);
}
}
}
#endregion
#region 自动编号
///
/// 在添加信息时自动计算编号.
///
///表名 ///字段名 /// 返回String对象
public String GetAutocoding(string TableName, string ID)
{
//查找指定表中ID号为最大的记录
SqlDataReader MyDR = MyDataClass.getcom("select max(" + ID + ") NID from " + TableName);
int Num = 0;
if (MyDR.HasRows) //当查找到记录时
{
MyDR.Read(); //读取当前记录
if (MyDR[0].ToString() == "")
return "0001";
Num = Convert.ToInt32(MyDR[0].ToString()); //将当前找到的最大编号转换成整数
++Num; //最大编号加1
string s = string.Format("{0:0000}", Num); //将整数值转换成指定格式的字符串
return s; //返回自动生成的编号
}
else
{
return "0001"; //当数据表没有记录时,返回0001
}
}
#endregion
#region 向comboBox控件传递数据表中的数据
///
/// 动态向comboBox控件的下拉列表添加数据.
///
///comboBox控件 ///数据表名称 public void CoPassData(ComboBox cobox, string TableName)
{
cobox.Items.Clear();
DataClass.MyMeans MyDataClsaa = new PWMS.DataClass.MyMeans();
SqlDataReader MyDR = MyDataClsaa.getcom("select * from " + TableName);
if (MyDR.HasRows)
{
while (MyDR.Read())
{
if (MyDR[1].ToString() != "" && MyDR[1].ToString()!=null)
cobox.Items.Add(MyDR[1].ToString());
}
}
}
#endregion
#region 向comboBox控件传递各省市的名称
///
/// 动态向comboBox控件的下拉列表添加省名.
///
///comboBox控件 ///SQL语句 ///字段位数 public void CityInfo(ComboBox cobox, string SQLstr, int n)
{
cobox.Items.Clear();
DataClass.MyMeans MyDataClsaa = new PWMS.DataClass.MyMeans();
SqlDataReader MyDR = MyDataClsaa.getcom(SQLstr);
if (MyDR.HasRows)
{
while (MyDR.Read())
{
if (MyDR[n].ToString() != "" && MyDR[n].ToString() != null)
cobox.Items.Add(MyDR[n].ToString());
}
}
}
#endregion
#region 将日期转换成指定的格式
///
/// 将日期转换成yyyy-mm-dd格式.
///
///日期 /// 返回String对象
public string Date_Format(string NDate)
{
string sm,sd;
int y, m, d;
try
{
y = Convert.ToDateTime(NDate).Year;
m = Convert.ToDateTime(NDate).Month;
d = Convert.ToDateTime(NDate).Day;
}
catch
{
return "";
}
if (y == 1900)
return "";
if (m < 10)
sm = "0" + Convert.ToString(m);
else
sm = Convert.ToString(m);
if (d < 10)
sd = "0" + Convert.ToString(d);
else
sd = Convert.ToString(d);
return Convert.ToString(y) + "-" + sm + "-" + sd;
}
#endregion
#region 将时间转换成指定的格式
///
/// 将时间转换成yyyy-mm-dd格式.
///
///日期 /// 返回String对象
public string Time_Format(string NDate)
{
string sh, sm, se;
int hh, mm, ss;
try
{
hh = Convert.ToDateTime(NDate).Hour;
mm = Convert.ToDateTime(NDate).Minute;
ss = Convert.ToDateTime(NDate).Second;
}
catch
{
return "";
}
sh = Convert.ToString(hh);
if (sh.Length < 2)
sh = "0" + sh;
sm = Convert.ToString(mm);
if (sm.Length < 2)
sm = "0" + sm;
se = Convert.ToString(ss);
if (se.Length < 2)
se = "0" + se;
return sh + sm + se;
}
#endregion
#region 设置MaskedTextBox控件的格式
///
/// 将MaskedTextBox控件的格式设为yyyy-mm-dd格式.
///
///日期 ///数据表名称 /// 返回String对象
public void MaskedTextBox_Format(MaskedTextBox MTBox)
{
MTBox.Mask = "0000-00-00";
MTBox.ValidatingType = typeof(System.DateTime);
}
#endregion
#region 用按钮控制数据记录移动时,改变按钮的可用状态
///
/// 设置按钮是否可用.
///
///首记录按钮 ///上一条记录按钮 ///下一条记录按钮 ///尾记录按钮 ///B1标识 ///B2标识 ///B3标识 ///B4标识 public void Ena_Button(Button B1, Button B2, Button B3, Button B4, int n1, int n2, int n3, int n4)
{
B1.Enabled = Convert.ToBoolean(n1);
B2.Enabled = Convert.ToBoolean(n2);
B3.Enabled = Convert.ToBoolean(n3);
B4.Enabled = Convert.ToBoolean(n4);
}
#endregion
#region 遍历清空指定的控件
///
/// 清空所有控件下的所有控件.
///
///可视化控件 public void Clear_Control(Control.ControlCollection Con)
{
foreach (Control C in Con){ //遍历可视化组件中的所有控件
if (C.GetType().Name == "TextBox") //判断是否为TextBox控件
if (((TextBox)C).Visible == true) //判断当前控件是否为显示状态
((TextBox)C).Clear(); //清空当前控件
if (C.GetType().Name == "MaskedTextBox") //判断是否为MaskedTextBox控件
if (((MaskedTextBox)C).Visible == true) //判断当前控件是否为显示状态
((MaskedTextBox)C).Clear(); //清空当前控件
if (C.GetType().Name == "ComboBox") //判断是否为ComboBox控件
if (((ComboBox)C).Visible == true) //判断当前控件是否为显示状态
((ComboBox)C).Text = ""; //清空当前控件的Text属性值
if (C.GetType().Name == "PictureBox") //判断是否为PictureBox控件
if (((PictureBox)C).Visible == true) //判断当前控件是否为显示状态
((PictureBox)C).Image = null; //清空当前控件的Image属性
}
}
#endregion
#region 保存添加或修改的信息
///
/// 保存添加或修改的信息.
///
///数据表中的所有字段 ///第一个字段值 ///第二个字段值 ///指定控件的数据集 ///要搜索的控件名称 ///数据表名称 ///控件的个数 ///标识,用于判断是添加还是修改 public void Part_SaveClass(string Sarr, string ID1, string ID2, Control.ControlCollection Contr, string BoxName, string TableName, int n, int m)
{
string tem_Field = "", tem_Value = "";
int p = 2;
if (m == 1){ //当m为1时,表示添加数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Field = "ID";
tem_Value = "'" + ID1 + "'";
p = 1;
}
else{
tem_Field = "Sut_id,ID";
tem_Value = "'" + ID1 + "','" + ID2 + "'";
}
}
else
if (m == 2){ //当m为2时,表示修改数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Value = "ID='" + ID1 + "'";
p = 1;
}
else
tem_Value = "Sut_ID='" + ID1 + "',ID='" + ID2 + "'";
}
if (m > 0){ //生成部份添加、修改语句
string[] Parr = Sarr.Split(Convert.ToChar(','));
for (int i = p; i < n; i++)
{
string sID = BoxName + i.ToString(); //通过BoxName参数获取要进行操作的控件名称
foreach (Control C in Contr){ //遍历控件集中的相关控件
if (C.GetType().Name == "TextBox" | C.GetType().Name == "MaskedTextBox" | C.GetType().Name == "ComboBox")
if (C.Name == sID){ //如果在控件集中找到相应的组件
string Ctext = C.Text;
if (C.GetType().Name == "MaskedTextBox") //如果当前是MaskedTextBox控件
Ctext = Date_Format(C.Text); //对当前控件的值进行格式化
if (m == 1){ //组合SQL语句中insert的相关语句
tem_Field = tem_Field + "," + Parr[i];
if (Ctext == "")
tem_Value = tem_Value + "," + "NULL";
else
tem_Value = tem_Value + "," + "'" + Ctext + "'";
}
if (m == 2)
{ //组合SQL语句中update的相关语句
if (Ctext=="")
tem_Value = tem_Value + "," + Parr[i] + "=NULL";
else
tem_Value = tem_Value + "," + Parr[i] + "='" + Ctext + "'";
}
}
}
}
ADDs = "";
if (m == 1) //生成SQL的添加语句
ADDs = "insert into " + TableName + " (" + tem_Field + ") values(" + tem_Value + ")";
if (m == 2) //生成SQL的修改语句
if (ID2 == "") //根据ID2参数,判断修改语句的条件
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID1 + "'";
else
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID2 + "'";
}
}
#endregion
#region 将当前表的数据信息显示在指定的控件上
///
/// 将DataGridView控件的当前记录显示在其它控件上.
///
///DataGridView控件 ///GroupBox控件的数据集 ///获取信息控件的部份名称 public void Show_DGrid(DataGridView DGrid, Control.ControlCollection GBox, string TName)
{
string sID = "";
if (DGrid.RowCount > 0)
{
for (int i = 2; i < DGrid.ColumnCount; i++)
{
sID = TName + i.ToString();
foreach (Control C in GBox)
{
if (C.GetType().Name == "TextBox" | C.GetType().Name == "MaskedTextBox" | C.GetType().Name == "ComboBox")
if (C.Name == sID)
{
if (C.GetType().Name != "MaskedTextBox")
C.Text = DGrid[i, DGrid.CurrentCell.RowIndex].Value.ToString();
else
C.Text = Date_Format(Convert.ToString(DGrid[i, DGrid.CurrentCell.RowIndex].Value).Trim());
}
}
}
}
}
#endregion
#region 清空控件集上的控件信息
///
/// 清空GroupBox控件上的控件信息.
///
///控件个数 ///GroupBox控件的数据集 ///获取信息控件的部份名称 public void Clear_Grids(int n, Control.ControlCollection GBox, string TName)
{
string sID = "";
for (int i = 2; i < n; i++)
{
sID = TName + i.ToString();
foreach (Control C in GBox)
{
if (C.GetType().Name == "TextBox" | C.GetType().Name == "MaskedTextBox" | C.GetType().Name == "ComboBox")
if (C.Name == sID)
{
C.Text = "";
}
}
}
}
#endregion
#region 控制数据表的显示字段
///
/// 通过条件显示相关表的字段,因使用DataGridView控件,添加System.Windows.Forms命名空间
///
///DataSet类 ///DataGridView控件 public void Correlation_Table(DataSet DSet, DataGridView DGrid)
{
DGrid.DataSource = DSet.Tables[0];
DGrid.Columns[0].Visible = false;
DGrid.Columns[1].Visible = false;
DGrid.RowHeadersVisible = false;
DGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
#endregion
#region 组合查询条件
///
/// 根据控件是否为空组合查询条件.
///
///GroupBox控件的数据集 ///获取信息控件的部份名称 ///查询关系 public void Find_Grids(Control.ControlCollection GBox, string TName, string ANDSign)
{
string sID = ""; //定义局部变量
if (FindValue.Length>0)
FindValue = FindValue + ANDSign;
foreach (Control C in GBox){ //遍历控件集上的所有控件
if (C.GetType().Name == "TextBox" | C.GetType().Name == "ComboBox"){ //判断是否要遍历的控件
if (C.GetType().Name == "ComboBox" && C.Text!=""){ //当指定控件不为空时
sID = C.Name;
if (sID.IndexOf(TName) > -1){ //当TName参数是当前控件名中的部分信息时
string[] Astr = sID.Split(Convert.ToChar('_')); //用“_”符号分隔当前控件的名称,获取相应的字段名
FindValue = FindValue + "(" + Astr[1] + " = '" + C.Text + "')" + ANDSign; //生成查询条件
}
}
if (C.GetType().Name == "TextBox" && C.Text != "") //如果当前为TextBox控件,并且控件不为空
{
sID = C.Name; //获取当前控件的名称
if (sID.IndexOf(TName) > -1) //判断TName参数值是否为当前控件名的子字符串
{
string[] Astr = sID.Split(Convert.ToChar('_')); //以“_”为分隔符,将控件名存入到一维数组中
string m_Sgin = ""; //用于记录逻辑运算符
string mID = ""; //用于记录字段名
if (Astr.Length > 2) //当数组的元素个数大于2时
mID = Astr[1] + "_" + Astr[2]; //将最后两个元素组成字段名
else
mID = Astr[1]; //获取当前条件所对应的字段名称
foreach (Control C1 in GBox) //遍历控件集
{
if (C1.GetType().Name == "ComboBox") //判断是否为ComboBox组件
if ((C1.Name).IndexOf(mID) > -1) //判断当前组件名是否包含条件组件的部分文件名
{
if (C1.Text == "") //当查询条件为空时
break; //退出本次循环
else
{
m_Sgin = C1.Text; //将条件值存储到m_Sgin变量中
break;
}
}
}
if (m_Sgin != "") //当该务件不为空时
FindValue = FindValue + "(" + mID + m_Sgin + C.Text + ")" + ANDSign; //组合SQL语句的查询条件
}
}
}
}
if (FindValue.Length > 0) //当存储查询条的变量不为空时,删除逻辑运算符AND和OR
{
if (FindValue.IndexOf("AND") > -1) //判断是否用AND连接条件
FindValue = FindValue.Substring(0, FindValue.Length - 4);
if (FindValue.IndexOf("OR") > -1) //判断是否用OR连接条件
FindValue = FindValue.Substring(0, FindValue.Length - 3);
}
else
FindValue = "";
}
#endregion
#region 判断字符型日期是否正确
///
/// 将字符型日期转换成日期进行判断.
///
///MaskedTextBox控件 ///字符型日期 /// <>
public bool Estimate_Date(MaskedTextBox MTbox)
{
try
{
DateTime DT = DateTime.Parse(MTbox.Text.Trim());
return true;
}
catch
{
MTbox.Text = "";
MessageBox.Show("日期输入错误,请重新输入!");
return false;
}
}
#endregion
#region 设置文本框只能输入数字型字符串
///
/// 文本框只能输入数字型和单精度型的字符串.
///
///KeyPressEventArgs类 ///文本框的字符串 ///标识,判断是数字型还是单精度型 public void Estimate_Key(KeyPressEventArgs e,string s,int n)
{
if (n==0) //只能输入整型
if (!(e.KeyChar <= '9' && e.KeyChar >= '0') && e.KeyChar != '\r' && e.KeyChar != '\b')
{
e.Handled = true; //处理KeyPress事件
}
if (n == 1) //可以输入整型或单精度型
{
if ((!(e.KeyChar <= '9' && e.KeyChar >= '0')) && e.KeyChar != '.' && e.KeyChar != '\r' && e.KeyChar != '\b')
{
e.Handled = true;
}
else
{
if (e.KeyChar == '.') //如果输入“.”
if (s == "") //当前文本框为空
e.Handled = true; //处理KeyPress事件
else
{
if (s.Length > 0) //当文本框不为空时
{
if (s.IndexOf(".") > -1) //查找是否已输入过“.”
e.Handled = true; //处理KeyPress事件
}
}
}
}
}
#endregion
#region 添加用户权限
///
/// 在添加用户时,将权限模版中的信息添加到用户权限表中.
///
///用户编号 ///权限值 /// <>
public void ADD_Pope(string ID,int n)
{
DataSet DSet;
DSet = MyDataClass.getDataSet("select PopeName from tb_PopeModel", "tb_PopeModel");
for (int i = 0; i < DSet.Tables[0].Rows.Count; i++)
{
MyDataClass.getsqlcom("insert into tb_UserPope (ID,PopeName,Pope) values('" + ID + "','" + Convert.ToString(DSet.Tables[0].Rows[i][0]) + "'," + n + ")");
}
}
#endregion
#region 清空所有数据表
///
/// 清空数据库中的所有数据表.
///
///GroupBox控件的数据集 ///获取信息控件的部份名称 public void Clear_Table(Control.ControlCollection GBox, string TName)
{
string sID = "";
foreach (Control C in GBox)
{
if (C.GetType().Name == "CheckBox")
{
sID = C.Name;
if (sID.IndexOf(TName) > -1)
{
if (((CheckBox)C).Checked == true)
{
string TableName = "";
string[] Astr = sID.Split(Convert.ToChar('_'));
TableName = "tb_" + Astr[1];
if (Astr[1].ToUpper() == ("Clew").ToUpper())
{
MyDataClass.getsqlcom("update " + TableName + " set Fate=0,Unlock=0 where ID>0");
}
else
{
MyDataClass.getsqlcom("Delete " + TableName);
if (Astr[1].ToUpper() == ("Login").ToUpper())
{
MyDataClass.getsqlcom("insert into " + TableName + " (ID,Name,Pass) values('0001','TSoft','111')");
ADD_Pope("0001", 1);
}
}
}
}
}
}
}
#endregion
#region 显示用户权限
///
/// 显示指定用户的权限.
///
///GroupBox控件的数据集 ///获取用户编号 public void Show_Pope(Control.ControlCollection GBox, string TID)
{
string sID = "";
string CheckName = "";
bool t = false;
DataSet DSet = MyDataClass.getDataSet("select ID,PopeName,Pope from tb_UserPope where ID='" + TID + "'", "tb_UserPope");
for (int i = 0; i < DSet.Tables[0].Rows.Count; i++)
{
sID = Convert.ToString(DSet.Tables[0].Rows[i][1]);
if ((int)(DSet.Tables[0].Rows[i][2]) == 1)
t = true;
else
t = false;
foreach (Control C in GBox)
{
if (C.GetType().Name == "CheckBox")
{
CheckName = C.Name;
if (CheckName.IndexOf(sID) > -1)
{
((CheckBox)C).Checked = t;
}
}
}
}
}
#endregion
#region 修改指定用户权限
///
/// 修改指定用户的权限.
///
///GroupBox控件的数据集 ///获取用户编号 public void Amend_Pope(Control.ControlCollection GBox, string TID)
{
string CheckName = "";
int tt = 0;
foreach (Control C in GBox)
{
if (C.GetType().Name == "CheckBox")
{
if (((CheckBox)C).Checked)
tt = 1;
else
tt = 0;
CheckName = C.Name;
string[] Astr = CheckName.Split(Convert.ToChar('_'));
MyDataClass.getsqlcom("update tb_UserPope set Pope=" + tt + " where (ID='" + TID + "') and (PopeName='" + Astr[1].Trim() + "')");
}
}
}
#endregion
#region 设置主窗体菜单不可用
///
/// 设置主窗体菜单不可用.
///
///MenuStrip控件 public void MainMenuF(MenuStrip MenuS)
{
string Men = "";
for (int i = 0; i < MenuS.Items.Count; i++)
{
Men = ((ToolStripDropDownItem)MenuS.Items[i]).Name;
if (Men.IndexOf("Menu") == -1)
((ToolStripDropDownItem)MenuS.Items[i]).Enabled = false;
ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i];
if (newmenu.HasDropDownItems && newmenu.DropDownItems.Count > 0)
for (int j = 0; j < newmenu.DropDownItems.Count; j++)
{
Men = newmenu.DropDownItems[j].Name;
if (Men.IndexOf("Menu") == -1)
newmenu.DropDownItems[j].Enabled = false;
ToolStripDropDownItem newmenu2 = (ToolStripDropDownItem)newmenu.DropDownItems[j];
if (newmenu2.HasDropDownItems && newmenu2.DropDownItems.Count > 0)
for (int p = 0; p < newmenu2.DropDownItems.Count; p++)
newmenu2.DropDownItems[p].Enabled = false;
}
}
}
#endregion
#region 根据用户权限设置主窗体菜单
///
/// 根据用户权限设置菜单是否可用.
///
///MenuStrip控件 ///当前登录用户名 public void MainPope(MenuStrip MenuS, String UName)
{
string Str = "";
string MenuName = "";
DataSet DSet = MyDataClass.getDataSet("select ID from tb_Login where Name='" + UName + "'", "tb_Login"); //获取当前登录用户的信息
string UID = Convert.ToString(DSet.Tables[0].Rows[0][0]); //获取当前用户编号
DSet = MyDataClass.getDataSet("select ID,PopeName,Pope from tb_UserPope where ID='" + UID + "'", "tb_UserPope"); //获取当前用户的权限信息
bool bo = false;
for (int k = 0; k < DSet.Tables[0].Rows.Count; k++) //遍历当前用户的权限名称
{
Str = Convert.ToString(DSet.Tables[0].Rows[k][1]); //获取权限名称
if (Convert.ToInt32(DSet.Tables[0].Rows[k][2]) == 1) //判断权限是否可用
bo = true;
else
bo = false;
for (int i = 0; i < MenuS.Items.Count; i++) //遍历菜单栏中的一级菜单项
{
ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i]; //记录当前菜单项下的所有信息
if (newmenu.HasDropDownItems && newmenu.DropDownItems.Count > 0) //如果当前菜单项有子级菜单项
for (int j = 0; j < newmenu.DropDownItems.Count; j++) //遍历二级菜单项
{
MenuName = newmenu.DropDownItems[j].Name; //获取当前菜单项的名称
if (MenuName.IndexOf(Str) > -1) //如果包含权限名称
newmenu.DropDownItems[j].Enabled = bo; //根据权限设置可用状态
ToolStripDropDownItem newmenu2 = (ToolStripDropDownItem)newmenu.DropDownItems[j]; //记录当前菜单项的所有信息
if (newmenu2.HasDropDownItems && newmenu2.DropDownItems.Count > 0) //如果当前菜单项有子级菜单项
for (int p = 0; p < newmenu2.DropDownItems.Count; p++) //遍历三级菜单项
{
MenuName = newmenu2.DropDownItems[p].Name; //获取当前菜单项的名称
if (MenuName.IndexOf(Str) > -1) //如果包含权限名称
newmenu2.DropDownItems[p].Enabled = bo; //根据权限设置可用状态
}
}
}
}
}
#endregion
#region 用TreeView控件调用StatusStrip控件下各菜单的单击事件
///
/// 用TreeView控件调用StatusStrip控件下各菜单的单击事件.
///
///MenuStrip控件 ///TreeView控件的TreeNodeMouseClickEventArgs类 public void TreeMenuF(MenuStrip MenuS, TreeNodeMouseClickEventArgs e)
{
string Men = "";
for (int i = 0; i < MenuS.Items.Count; i++) //遍历MenuStrip控件中主菜单项
{
Men = ((ToolStripDropDownItem)MenuS.Items[i]).Name; //获取主菜单项的名称
if (Men.IndexOf("Menu") == -1) //如果MenuStrip控件的菜单项没有子菜单
{
if (((ToolStripDropDownItem)MenuS.Items[i]).Text == e.Node.Text) //当节点名称与菜单项名称相等时
if (((ToolStripDropDownItem)MenuS.Items[i]).Enabled == false) //判断当前菜单项是否可用
{
MessageBox.Show("当前用户无权限调用" + "\"" + e.Node.Text + "\"" + "窗体");
break;
}
else
Show_Form(((ToolStripDropDownItem)MenuS.Items[i]).Text.Trim(), 1); //调用相应的窗体
}
ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i];
if (newmenu.HasDropDownItems && newmenu.DropDownItems.Count > 0) //遍历二级菜单项
for (int j = 0; j < newmenu.DropDownItems.Count; j++)
{
Men = newmenu.DropDownItems[j].Name; //获取二级菜单项的名称
if (Men.IndexOf("Menu") == -1)
{
if ((newmenu.DropDownItems[j]).Text == e.Node.Text)
if ((newmenu.DropDownItems[j]).Enabled == false)
{
MessageBox.Show("当前用户无权限调用" + "\"" + e.Node.Text + "\"" + "窗体");
break;
}
else
Show_Form((newmenu.DropDownItems[j]).Text.Trim(), 1);
}
ToolStripDropDownItem newmenu2 = (ToolStripDropDownItem)newmenu.DropDownItems[j];
if (newmenu2.HasDropDownItems && newmenu2.DropDownItems.Count > 0) //遍历三级菜单项
for (int p = 0; p < newmenu2.DropDownItems.Count; p++)
{
if ((newmenu2.DropDownItems[p]).Text == e.Node.Text)
if ((newmenu2.DropDownItems[p]).Enabled == false)
{
MessageBox.Show("当前用户无权限调用" + "\"" + e.Node.Text + "\"" + "窗体");
break;
}
else
if ((newmenu2.DropDownItems[p]).Text.Trim() == "员工生日提示" || (newmenu2.DropDownItems[p]).Text.Trim() == "员工合同提示")
Show_Form((newmenu2.DropDownItems[p]).Text.Trim(), 1);
else
Show_Form((newmenu2.DropDownItems[p]).Text.Trim(), 2);
}
}
}
}
#endregion
#region 查询指定范围内生日与合同到期的职工
///
/// 查询指定范围内生日与合同到期的职工.
///
///标识,判断查询的是生日,还是合同 public void PactDay(int i)
{
DataSet DSet = MyDataClass.getDataSet("select * from tb_Clew where kind=" + i + " and unlock=1", "tb_clew");
if (DSet.Tables[0].Rows.Count > 0)
{
string Vfield = "";
string dSQL = "";
int sday = Convert.ToInt32(DSet.Tables[0].Rows[0][1]);
if (i == 1)
{
Vfield = "Birthday";
dSQL = "select * from tb_Stuffbusic where (datediff(day,getdate(),convert(Nvarchar(12),cast (cast (year(getdate()) as char(4))+'-'+ cast(month(" + Vfield + ") as char(2))+'-'+ cast (day(" + Vfield + ") as char(2)) as datetime),110))<=" + sday + ") and (datediff(day,getdate(),convert(Nvarchar(12),cast (cast (year(getdate()) as char(4))+'-'+ cast(month(" + Vfield + ") as char(2))+'-'+cast (day(" + Vfield + ") as char(2)) as datetime),110))>=0)";
}
else
{
Vfield = "Pact_E";
dSQL = "select * from tb_Stuffbusic where ((getdate()-convert(Nvarchar(12)," + Vfield + ",110))>=-" + sday + " and (getdate()-convert(Nvarchar(12)," + Vfield + ",110))<=0)";
}
DSet = MyDataClass.getDataSet(dSQL, "tb_Stuffbusic");
if (DSet.Tables[0].Rows.Count > 0)
{
if (i == 1)
Vfield = "是否查看" + sday.ToString() + "天内过生日的职工信息?";
else
Vfield = "是否查看" + sday.ToString() + "天内合同到期的职工信息?";
if (MessageBox.Show(Vfield, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
DataClass.MyMeans.AllSql = dSQL;
Show_Form("人事档案浏览", 1);
}
}
}
}
#endregion
#region 将图片存储到数据库中
///
/// 以二进制的形式将图片存储到数据库中.
///
///职工编号 ///图片的二进制形式 public void SaveImage(string MID, byte[] p)
{
MyDataClass.con_open();
StringBuilder strSql = new StringBuilder();
strSql.Append("update tb_Stuffbusic Set Photo=@Photo where ID="+MID);
SqlCommand cmd = new SqlCommand(strSql.ToString(), DataClass.MyMeans.My_con);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = p;
cmd.ExecuteNonQuery();
DataClass.MyMeans.My_con.Close();
}
#endregion
}
}
本文涉及源代码下载