此文仅为记录在校期间windows应用开发课程学习成果,本项目仍存在很多不足,仅供参考学习使用。
本门课程大作业要求完成一个具有主界面、用户登录、注册、注销、重新登陆、员工信息增删改查等功能的系统。其中有10个类,分别是:
主函数类Program, 数据库类DBAccess,操作类Moudules,登录窗口LoginDialog,注册窗口RegisterWindow,主界面MainWindow,员工管理界面StuffForm,用户中心界面UserCenter,,修改密码窗口changePwdWindows,注销窗口LogoutAccount。
以下是代码实现及窗体:
program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace HRMS
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
//Application.Run(new StuffForm());
}
}
}
DBAccess:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace HRMS
{
class DBAccess
{
#region 公共变量
public static int nLoginID = 0;
public static string strLoginName;
public static string strSQLSelectAll = "select * from tb_StuffBasic";
#endregion
//连接对象、连接字符串、SQL命令对象、SQL命令字符串、reader对象、dataset对象
//在此添加你的数据库信息
//string strConnection = "Data Source= ;Database= ;User id=sa;PWD=";
SqlConnection sqlConnection;
private static DBAccess dbAccess;
//连接、发送命令、获取结果
public static DBAccess GetInstance()
{
if (dbAccess == null)
{
dbAccess = new DBAccess();
}
return dbAccess;
}
public SqlConnection GetConnection()
{
try
{
sqlConnection = new SqlConnection(strConnection);
sqlConnection.Open();
if (sqlConnection.State == System.Data.ConnectionState.Open)
{
return sqlConnection;
}
return null;
}
catch (Exception)
{
throw;
}
}
public void CloseConnection()
{
if (sqlConnection.State == System.Data.ConnectionState.Open)
{
sqlConnection.Close();
sqlConnection.Dispose(); //释放
}
}
public void GetSQLCommand(string strCmd, string strParam, byte[] bytes)
{
GetConnection();
SqlCommand sqlCommand = new SqlCommand(strCmd, sqlConnection);
//@Param
sqlCommand.Parameters.Add(strParam, SqlDbType.Binary).Value = bytes;
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
CloseConnection();
}
public void GetSQLCommand(string str) //不用返回命令
{
GetConnection();
SqlCommand sqlCommand = new SqlCommand(str, sqlConnection); // 命令、链接
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
CloseConnection();
}
public SqlDataReader GetReaderFormCommand(string strCmd) // 返回reader对象
{
SqlDataReader reader = null;
GetConnection();
SqlCommand sqlCommand = new SqlCommand(strCmd, sqlConnection);
reader = sqlCommand.ExecuteReader();
sqlCommand.Dispose();
//CloseConnection(); 登录时reader还要用,不能关闭连接
return reader;
}
public DataSet GetDataSet(string strCmd, string tableName) //返回数据集
{
GetConnection();
SqlDataAdapter dataAdapter = new SqlDataAdapter(strCmd, sqlConnection);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, tableName);
return dataSet;
}
}
}
Moudules:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace HRMS
{
class Modules
{
const string strDefaultPic = "default.jpg";
public static Image ReadDefaultImage()
{
Image image = null;
try
{
image = Image.FromFile(strDefaultPic);
}
catch (Exception)
{
throw;
}
return image;
}
public static byte[] Image2Bytes(Image image)
{
byte[] bytes;
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
public static Image Bytes2Image(byte[] bytes)
{
Image image = null;
MemoryStream ms = new MemoryStream(bytes);
image = Image.FromStream(ms);
return image;
}
}
}
LoginDialog:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace HRMS
{
public partial class LoginDialog : Form
{
public LoginDialog()
{
InitializeComponent();
}
//Tag: 1-未登录;2-登录;3-重新登录。
private void cancleButton_Click(object sender, EventArgs e)
{
if ((int)this.Tag == 1)
{
Application.Exit();
}
else
{
this.Close();
}
}
private void loginButton_Click(object sender, EventArgs e)
{
//连接数据库、构造命令、执行命令、根据返回结果判断是否允许登录
//SqlConnection conn = new SqlConnection(strConnection);
//conn.Open();
//if (conn.State == ConnectionState.Open)
//{
// SqlCommand command = new SqlCommand();
// command.Connection = conn;
// command.CommandText = "select * from tb_Login where Name='" + nameTextBox.Text.Trim() + "'and Password='" + codeTextBox.Text.Trim() + "'";
// command.CommandType = CommandType.Text;
// SqlDataReader reader = command.ExecuteReader();
// //SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand();
string str = "select * from tb_Login where Name='" + nameTextBox.Text.Trim() + "'and Password='" + codeTextBox.Text.Trim() + "'";
SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand(str);
if (!reader.HasRows)
{
MessageBox.Show("账号或密码错误,请重试!");
}
else
{
reader.Read();
DBAccess.strLoginName = (string)reader[1];
this.Tag = 2;
this.Close();
}
//}
}
private void registerButton_Click(object sender, EventArgs e)
{
RegisterWindow registerWindow = new RegisterWindow();
registerWindow.ShowDialog();
}
private void LoginDialog_Load(object sender, EventArgs e)
{
}
}
}
RegisterWindow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace HRMS
{
public partial class RegisterWindow : Form
{
public RegisterWindow()
{
InitializeComponent();
}
string strSQLCommand;
DBAccess dbAccess = new DBAccess();
private void sureRegisterButton_Click(object sender, EventArgs e)
{
if (rNameTextBox.Text.Trim() != "")
{
int flag = 1;
string str = "select Name from tb_Login where Name ='"
+ rNameTextBox.Text.Trim() + "'";
SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand(str);
if (reader.HasRows)
{
MessageBox.Show("用户名已存在,请重新输入!");
flag = 0;
}
if (rCodeTextBox.Text.IndexOf(" ") >= 0)
{
MessageBox.Show("密码不得含有空格,请重新输入!");
flag = 0;
}
if (6 > rCodeTextBox.Text.Trim().Length || rCodeTextBox.Text.Trim().Length > 16)
{
MessageBox.Show("密码长度不符合规定,请重新输入!");
flag = 0;
}
if (rCodeTextBox.Text != sureCodeTextBox.Text)
{
MessageBox.Show("两次密码输入不一致,请重新输入!");
flag = 0;
}
if (checkBox.Checked == false)
{
MessageBox.Show("您还未阅读并同意公司下发的相关资料文件内容!");
flag = 0;
}
if (flag == 1)
{
//insert into tb_StuffBasic() values()
string strColumns = "Name, Password";
strSQLCommand = "insert into tb_Login(" + strColumns + ") values('"
+ rNameTextBox.Text.Trim() + "','"
+ rCodeTextBox.Text.Trim() + "')";
dbAccess.GetSQLCommand(strSQLCommand);
insert into tb_Question() values()
//strSQLCommand = "insert into tb_Question(" + "Name" + ")"
// + " values('" + DBAccess.strLoginName + "')";
//dbAccess.GetSQLCommand(strSQLCommand);
DialogResult dr = MessageBox.Show("注册成功!");
if (dr == DialogResult.OK)
{
this.Close();
}
}
}
else
{
MessageBox.Show("用户名不得为空,请输入用户名!");
}
}
private void button1_Click(object sender, EventArgs e)
{
rNameTextBox.Text = "";
rCodeTextBox.Text = "";
sureCodeTextBox.Text = "";
checkBox.Checked = false;
}
private void RegisterWindow_Load(object sender, EventArgs e)
{
}
}
}
MainWindow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HRMS
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_Load(object sender, EventArgs e)
{
LoginDialog loginDialog = new LoginDialog();
loginDialog.Tag = 1;
loginDialog.ShowDialog();
if ((int)loginDialog.Tag != 2)
{
Application.Exit();
}
userStatusStrip.Items[0].Text = "欢迎 " + DBAccess.strLoginName + " 使用";
}
private void 重新登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
LoginDialog loginDialog = new LoginDialog();
loginDialog.Tag = 2;
loginDialog.ShowDialog();
}
private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 员工信息ToolStripMenuItem_Click(object sender, EventArgs e) //
{
StuffForm stuffForm = new StuffForm();
stuffForm.ShowDialog();
}
private void 用户中心ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
UserCenter userCenter = new UserCenter();
userCenter.ShowDialog();
}
}
}
StuffForm:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace HRMS
{
public partial class StuffForm : Form
{
public StuffForm()
{
InitializeComponent();
}
enum OPERATIONTYPE //定义枚举类型
{
Add,
Update,
Delete,
Save,
Cancel,
None
}
OPERATIONTYPE operationType = OPERATIONTYPE.None; //定义一个操作类
DBAccess dbAccess = DBAccess.GetInstance(); // 创建对象
DataSet dataset;
string strSQLCommand;
string strSQLParam = "@Param"; //临时变量
private void StuffForm_Load(object sender, EventArgs e) //创建之后,现实之前进行格子初始化
{
try
{
EnableButtons(operationType);
dataset = dbAccess.GetDataSet(DBAccess.strSQLSelectAll, "tb_StuffBasic");
dataGridView1.DataSource = dataset.Tables[0]; //把这张表全部显示
dataGridView1.Columns[0].Width = 60;
dataGridView1.Columns[1].Width = 60;
dataGridView1.Columns[2].Width = 60;
dataGridView1.Columns[3].Width = 100;
dataGridView1.Columns[4].Width = 100;
dataGridView1.Columns[5].Width = 100;
}
catch (Exception)
{
throw;
}
//下拉框
fieldComboBox.Items.Clear();
fieldComboBox.Items.Add("ID");
fieldComboBox.Items.Add("Name");
fieldComboBox.Items.Add("Sex");
fieldComboBox.Items.Add("Phone");
fieldComboBox.SelectedIndex = 0;
}
private void searchButton_Click(object sender, EventArgs e) //查询
{
//select * from tb_StuffBasic where ID='2'
if (valueTextBox.Text.Trim() == "")
{
dataset = dbAccess.GetDataSet(DBAccess.strSQLSelectAll, "tb_StuffBasic");
dataGridView1.DataSource = dataset.Tables[0];
}
else
{
string strCmd = "select * from tb_StuffBasic where "
+ fieldComboBox.SelectedItem.ToString()
+ "='" + valueTextBox.Text.Trim()
+ "'";
dataset = dbAccess.GetDataSet(strCmd, "tb_StuffBasic");
dataGridView1.DataSource = dataset.Tables[0];
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) //在下面列出对应信息
{
byte[] pic;
if (dataGridView1.RowCount > 1)
{
int row = dataGridView1.CurrentCell.RowIndex;
idTextBox.Text = dataGridView1[0, row].Value.ToString();
nameTextBox.Text = dataGridView1[1, row].Value.ToString();
sexTextBox.Text = dataGridView1[2, row].Value.ToString();
positionTextBox.Text = dataGridView1[3, row].Value.ToString();
phoneTextBox.Text = dataGridView1[4, row].Value.ToString();
addressTextBox.Text = dataGridView1[5, row].Value.ToString();
emailTextBox.Text = dataGridView1[6, row].Value.ToString();
//显示图片
try
{
if (!(dataset.Tables[0].Rows[row][7] is DBNull)) //如果图片不为空
{
pic = (byte[])(dataset.Tables[0].Rows[row][7]); // 从数据集中取图片字节序列
MemoryStream ms = new MemoryStream(pic); // 将字节序列转成流
photoPictureBox.Image = Image.FromStream(ms); //从流里创建照片
}
else
{
photoPictureBox.Image = null;
}
}
catch (Exception)
{
throw;
}
currentTextBox.Text = nameTextBox.Text; // 右上角文本框显示当前用户
}
}
private void EnableButtons(OPERATIONTYPE type) //设置按钮使能
{
switch (type)
{
case OPERATIONTYPE.Add:
case OPERATIONTYPE.Update:
addButton.Enabled = false;
modifyButton.Enabled = false;
delButton.Enabled = false;
saveButton.Enabled = true;
cancelButton.Enabled = true;
picSelectButton.Enabled = true;
picDeleteButton.Enabled = true;
break;
case OPERATIONTYPE.Delete:
case OPERATIONTYPE.Save:
case OPERATIONTYPE.Cancel:
case OPERATIONTYPE.None:
default:
addButton.Enabled = true;
modifyButton.Enabled = true;
delButton.Enabled = true;
saveButton.Enabled = false;
cancelButton.Enabled = true;
picSelectButton.Enabled = false;
picDeleteButton.Enabled = false;
break;
}
}
private void ClearTextBoxes()
{
foreach (var control in panel2.Controls)
{
if (control is TextBox)
{
((TextBox)control).Clear();
}
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
//设置按钮使能
operationType = OPERATIONTYPE.None;
EnableButtons(OPERATIONTYPE.None);
//清空文本框
ClearTextBoxes();
}
private void addButton_Click(object sender, EventArgs e) //添加按钮
{
operationType = OPERATIONTYPE.Add;
EnableButtons(OPERATIONTYPE.Add);
ClearTextBoxes();
photoPictureBox.Image = Modules.ReadDefaultImage();
}
private void modifyButton_Click(object sender, EventArgs e) //修改按钮
{
operationType = OPERATIONTYPE.Update;
EnableButtons(operationType);
}
private void ConstructAddingString() //构造添加的字符串
{
//insert into tb_StuffBasic() values()
string strColumns = "Name, Sex, Position, Phone, Address, Email, Photo";
strSQLCommand = "insert into tb_StuffBasic(" + strColumns + ") values('"
+ nameTextBox.Text.Trim() + "','"
+ sexTextBox.Text.Trim() + "','"
+ positionTextBox.Text.Trim() + "','"
+ phoneTextBox.Text.Trim() + "','"
+ addressTextBox.Text.Trim() + "','"
+ emailTextBox.Text.Trim() + "',"
+ strSQLParam + ")";
}
private void ConstructUpdatingString() //构造修改的字符串
{
//update tb_StuffBasic set Name='aa'where ID='2';
//"Name, Sex, Position, Phone, Address, Email, Photo"
strSQLCommand = "update tb_StuffBasic set Name='"
+ nameTextBox.Text.Trim() + "',Sex='"
+ sexTextBox.Text.Trim() + "',Position='"
+ positionTextBox.Text.Trim() + "',Phone='"
+ phoneTextBox.Text.Trim() + "',Address='"
+ addressTextBox.Text.Trim() + "',Email='"
+ emailTextBox.Text.Trim() + "',Photo="
+ strSQLParam + " where ID='"
+ idTextBox.Text.Trim() + "'";
}
private void UpdateDataSet() //更新显示的表格
{
dataset = dbAccess.GetDataSet(DBAccess.strSQLSelectAll, "tb_StuffBasic");
dataGridView1.DataSource = dataset.Tables[0];
}
private void saveButton_Click(object sender, EventArgs e) //保存按钮
{
if (operationType == OPERATIONTYPE.Add) //如果状态是添加
{
//正则表达式 ^ 是开始 $是结束 1表示开始数字为1
//第一个\为转义 \b表示匹配一个整数 {10}表示匹配10次
var re = @"^1\d{10}$";
if (System.Text.RegularExpressions.Regex.IsMatch(phoneTextBox.Text, re))
{
//构造SQL命令字符串,执行该命令
ConstructAddingString();
byte[] bytes = Modules.Image2Bytes(photoPictureBox.Image);
dbAccess.GetSQLCommand(strSQLCommand, strSQLParam, bytes);
UpdateDataSet();
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("手机号码错误,请检查后重新输入!","提示");
EnableButtons(OPERATIONTYPE.Add);
return;
}
}
else //如果状态是修改
{
var re = @"^1\d{10}$";
if (System.Text.RegularExpressions.Regex.IsMatch(phoneTextBox.Text, re))
{
//构造SQL命令字符串,执行该命令
ConstructUpdatingString();
byte[] bytes = Modules.Image2Bytes(photoPictureBox.Image);
dbAccess.GetSQLCommand(strSQLCommand, strSQLParam, bytes);
UpdateDataSet();
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("手机号码错误,请检查后重新输入!", "提示");
EnableButtons(OPERATIONTYPE.Add);
return;
}
}
operationType = OPERATIONTYPE.None;
EnableButtons(operationType);
}
private void delButton_Click(object sender, EventArgs e) //删除按钮
{
if (MessageBox.Show("您真的要删除吗?此删除不可恢复", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
{
//delete from tb_StuffBasic where ID='2'
strSQLCommand = "delete from tb_StuffBasic where ID='"
+ idTextBox.Text.Trim() + "'";
dbAccess.GetSQLCommand(strSQLCommand);
UpdateDataSet();
}
else
{
return;
}
}
private void picSelectButton_Click(object sender, EventArgs e) //选择图片
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
string strFileName;
dialog.Filter = "*.jpg|*.jpg|*.bmp|*.bmp"; //文本显示的格式,过滤格式
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
strFileName = dialog.FileName;
photoPictureBox.Image = Image.FromFile(strFileName);
}
}
}
private void picDeleteButton_Click(object sender, EventArgs e) //删除图片
{
photoPictureBox.Image = Modules.ReadDefaultImage();
}
}
}
userCenter:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace HRMS
{
public partial class UserCenter : Form
{
public UserCenter()
{
InitializeComponent();
}
DBAccess dbAccess = DBAccess.GetInstance();
private void UserCenter_Load(object sender, EventArgs e)
{
nameLabel.Text = DBAccess.strLoginName;
string strName = "select Name from tb_StuffBasic where Name ='"
+ DBAccess.strLoginName + "'";
SqlDataReader readerName = DBAccess.GetInstance().GetReaderFormCommand(strName);
if (!readerName.HasRows)
{
sexLabel.Text = "暂未录入";
phoneLabel.Text = "暂未录入";
positionLabel.Text = "暂未录入";
addressLabel.Text = "暂未录入";
emailLabel.Text = "暂未录入";
}
else
{
string str = "select * from tb_StuffBasic where Name ='"
+ DBAccess.strLoginName + "'";
SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand(str);
reader.Read();
sexLabel.Text = (string)reader[2];
phoneLabel.Text = (string)reader[3];
positionLabel.Text = (string)reader[4];
addressLabel.Text = (string)reader[5];
emailLabel.Text = (string)reader[6];
byte[] pic;
pic = (byte[])(reader[7]);
MemoryStream ms = new MemoryStream(pic);
photoPictureBox.Image = Image.FromStream(ms);
}
}
private void changePwdButton_Click(object sender, EventArgs e)
{
changePwdWindow changePwdWindow = new changePwdWindow();
changePwdWindow.ShowDialog();
}
private void logoutAccButton_Click(object sender, EventArgs e)
{
LogoutAccount logoutAccount = new LogoutAccount();
logoutAccount.ShowDialog();
}
}
}
changePwdWindow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace HRMS
{
public partial class changePwdWindow : Form
{
public changePwdWindow()
{
InitializeComponent();
}
private void changePwdWindow_Load(object sender, EventArgs e)
{
nameTextBox.Text = DBAccess.strLoginName;
}
int temp;
string strSQLCommand;
DBAccess dbAccess = DBAccess.GetInstance();
private void changeButton_Click(object sender, EventArgs e)
{
changeButton.Text = "看不清?换一张";
string[] imgName = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg" };
Random random = new Random();
int index = random.Next(0, imgName.Length);
temp = index;
checkPictureBox.Image = Image.FromFile(Application.StartupPath + "\\imgs" + "\\" + imgName[index]);
}
private string seek(int temp)
{
string[] imgDic = { "fcgt", "frar", "grer", "mecc", "oxfa", "qrfa", "tsux", "txnx", "umwa", "yeqe" };
string result = imgDic[temp];
return result;
}
private void sureButton_Click(object sender, EventArgs e)
{
int flag = 1;
string str = "select PassWord from tb_Login where Name ='" + nameTextBox.Text.Trim() + "'";
SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand(str);
reader.Read();
string pwd = (string)reader[0];
if (pwd != oldCodeTextBox.Text)
{
MessageBox.Show("旧密码输入错误,请重新输入!");
flag = 0;
}
if (pwd == newCodeTextBox.Text)
{
MessageBox.Show("新密码不得与旧密码相同,请重新输入!");
flag = 0;
}
if (newCodeTextBox.Text.IndexOf(" ") >= 0)
{
MessageBox.Show("新密码不得含有空格,请重新输入!");
flag = 0;
}
if (6 > newCodeTextBox.Text.Trim().Length || newCodeTextBox.Text.Trim().Length > 16)
{
MessageBox.Show("新密码长度不符合规定,请重新输入!\n请设置6-16位密码,不得含有空格!");
flag = 0;
}
if (newCodeTextBox.Text != sureNewCodeTextBox.Text)
{
MessageBox.Show("两次密码输入不一致,请重新输入!");
flag = 0;
}
string yz = yzTextBox.Text.ToLower().Trim();
if (yz != seek(temp))
{
MessageBox.Show("图片验证码输入错误,请重新输入!");
flag = 0;
}
if (flag == 1)
{
//update tb_StuffBasic set Name='aa'where ID='2';
//"Name, Password"
strSQLCommand = "update tb_Login set Password='"
+ newCodeTextBox.Text.Trim() + "' where Name='"
+ nameTextBox.Text.Trim() + "'";
dbAccess.GetSQLCommand(strSQLCommand);
DialogResult dr = MessageBox.Show("密码修改成功!");
if (dr == DialogResult.OK)
{
this.Close();
}
}
}
}
}
LogoutAccount:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace HRMS
{
public partial class LogoutAccount : Form
{
public LogoutAccount()
{
InitializeComponent();
}
DBAccess dbAccess = DBAccess.GetInstance();
string strSQLCommand;
DataSet dataset;
private void UpdateDataSet() //更新数据库
{
dataset = dbAccess.GetDataSet(DBAccess.strSQLSelectAll, "tb_Login");
}
private void logoutButton_Click(object sender, EventArgs e)
{
string str = "select * from tb_Login where Name='"
+ nameTextBox.Text.Trim() + "'and Password='"
+ codeTextBox.Text.Trim() + "'";
SqlDataReader reader = DBAccess.GetInstance().GetReaderFormCommand(str);
if (!reader.HasRows)
{
MessageBox.Show("账号或密码错误,请重试!");
}
else
{
DialogResult dr = MessageBox.Show("是否确定注销账号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (dr == DialogResult.OK)
{
//delete from tb_Login where ID='2'
strSQLCommand = "delete from tb_Login where Name='"
+ nameTextBox.Text.Trim() + "'";
dbAccess.GetSQLCommand(strSQLCommand);
UpdateDataSet();
Application.Exit();
}
}
}
private void LogoutAccount_Load(object sender, EventArgs e)
{
nameTextBox.Text = DBAccess.strLoginName;
}
}
}