📄 form_personmanage.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
namespace AttendManager
{
public partial class Form_PersonManage : Form
{
private Form_Main m_FromMain=null;
private OdbcDataAdapter m_oAdapter;
private OdbcConnection m_connection;
private OdbcCommand m_oCMD;
public Form_PersonManage(Form_Main mFrm)
{
InitializeComponent();
m_FromMain = mFrm;
m_oAdapter = new OdbcDataAdapter();
m_oCMD = new OdbcCommand();
m_oCMD.CommandText = "Select * From 员工信息";//ID,用户名,口令,用户类型ID,附注
//打开连接
m_connection = new OdbcConnection("Dsn=" + Properties.Settings.Default.DNS);
m_oCMD.Connection = m_connection;
m_oAdapter.SelectCommand = m_oCMD;
}
private void Form_PersonManage_Load(object sender, EventArgs e)
{
try
{
m_connection = new OdbcConnection("Dsn=" + Properties.Settings.Default.DNS);
m_connection.Open();
GetUsers();
SetDataView();
this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
this.textBox_name.DataBindings.Add("Text", bindingSource1, "姓名");
this.textBox_depart.DataBindings.Add("Text", bindingSource1, "部门");
this.textBox_usertype.DataBindings.Add("Text", bindingSource1, "员工类型ID");
this.textBox_CardNO.DataBindings.Add("Text", bindingSource1, "卡号");
this.textBox_CardPW.DataBindings.Add("Text", bindingSource1, "密码");
this.textBox_conntext.DataBindings.Add("Text", bindingSource1, "附注");
this.button_add.Enabled = m_FromMain.isEditer();
this.button_del.Enabled = m_FromMain.isEditer();
this.button_update.Enabled = m_FromMain.isEditer();
this.button_new.Enabled = m_FromMain.isEditer();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "IN Form_ProtocolManage_Load()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void Form_PersonManage_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
m_FromMain.m_fPM = null;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void SetDataView()
{
try
{
DataSet ds=new DataSet();
m_oAdapter.Fill(ds, "员工信息");
this.dataGridView1.DataSource = ds.Tables["员工信息"];
this.dataGridView1.Columns[6].Visible = false;
bindingSource1.DataSource = ds;
bindingSource1.DataMember = "员工信息";
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "IN SetDataView()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void GetUsers()
{
try
{
// Execute the DataReader and access the data.
string queryString = "Select * From 员工类型";
OdbcCommand command = new OdbcCommand(queryString, m_FromMain.m_connection);
OdbcDataReader reader = command.ExecuteReader();
BindingSource bs = new BindingSource();
bs.DataSource = reader;
this.comboBox_Pertype.DataSource = bs;
this.comboBox_Pertype.ValueMember = "类型ID";
this.comboBox_Pertype.DisplayMember = "类型说明";
reader.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "IN GetUsers()",MessageBoxButtons.OK ,MessageBoxIcon.Error );
}
finally
{
}
}
private void comboBox_usertype_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//MessageBox.Show(comboBox_usertype.SelectedValue.ToString () );
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
MessageBox.Show(ex.Message, "IN comboBox_usertype_SelectedIndexChanged()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void button_update_Click(object sender, EventArgs e)
{
try
{
//首先判断添加的信息是否完整
if (this.textBox_name.Text != "" & this.comboBox_Pertype.SelectedValue != null)
{
if (this.textBox_depart.Text == "") { this.textBox_depart.Text = " "; }
if (this.textBox_job.Text == "") { this.textBox_job.Text = " "; }
if (this.textBox_CardNO.Text == "") { this.textBox_CardNO.Text = " "; }
if (this.textBox_CardPW.Text == "") { this.textBox_CardPW.Text = " "; }
if (this.textBox_conntext.Text == "") { this.textBox_conntext.Text = " "; }
//更新记录的SQL语句
string update = " UPDATE 员工信息 SET ";
update += " 部门='";
update += this.textBox_depart.Text + "', 职位='";
update += this.textBox_job.Text + "', 员工类型ID =";
update += this.comboBox_Pertype.SelectedValue + ", 卡号 ='";
update += this.textBox_CardNO.Text + "', 密码 ='";
update += this.textBox_CardPW.Text + "', 附注 ='";
update += this.textBox_conntext.Text + "' WHERE 用户名='";
update += this.textBox_name.Text + "'";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = update;
comm.Connection = m_connection;
if (m_connection.State == ConnectionState.Closed) { m_connection.Open(); }
//执行添加记录的语句
comm.ExecuteNonQuery();
SetDataView();
//成功提示
MessageBox.Show("修改记录成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//错误提示
MessageBox.Show("请将字段信息输入完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
MessageBox.Show(ex.Message, "IN button_update_Click()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void textBox_usertype_TextChanged(object sender, EventArgs e)
{
try
{
//this.comboBox_usertype.SelectedItem = textBox_usertype.Text;
this.comboBox_Pertype.SelectedValue = int.Parse(this.textBox_usertype.Text);
//this.comboBox_usertype.SelectedValue=textBox_usertype.Text;
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
MessageBox.Show(ex.Message, "IN textBox_usertype_TextChanged()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void button_add_Click(object sender, EventArgs e)
{
try
{
//首先判断添加的信息是否完整
if (this.textBox_name.Text != "" & this.comboBox_Pertype.SelectedValue !=null )
{
//添加记录时的SQL命令
string insert = " INSERT INTO 员工信息 ( 姓名 , 部门 , 职位 ,员工类型ID ,卡号 , 密码 , 附注) VALUES ('";
insert += this.textBox_name.Text + "', '";
insert += this.textBox_depart.Text + "', ";
insert += this.textBox_job.Text + "', ";
insert += this.comboBox_Pertype.SelectedValue + ",'";
insert += this.textBox_CardNO.Text + "', '";
insert += this.textBox_CardPW.Text + "', '";
insert += this.textBox_conntext.Text + "') ";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = insert;
comm.Connection = m_connection;
if (m_connection.State == ConnectionState.Closed) { m_connection.Open(); }
//执行添加记录的语句
comm.ExecuteNonQuery();
SetDataView();
//
// //更新数据集
// this.dataSet1.Tables["information"].Rows[this.myBind.Position].BeginEdit();
// this.dataSet1.Tables["information"].Rows[this.myBind.Position].EndEdit();
// this.dataSet1.AcceptChanges();
//
// //成功提示
// MessageBox.Show("添加记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
// this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
}
else
{
//错误提示
MessageBox.Show("请将字段信息输入完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception error)
{
//错误提示
MessageBox.Show("添加记录发生错误:" + error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.textBox_name.Text = "";
this.textBox_depart.Text = "" ;
this.textBox_job.Text = "";
this.comboBox_Pertype.SelectedValue = 1;
this.textBox_CardNO.Text = "";
this.textBox_CardPW.Text = "";
this.textBox_conntext.Text = "";
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "IN button1_Click()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void button_del_Click(object sender, EventArgs e)
{
try
{
//确认删除记录
if (MessageBox.Show("是否删除记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
try
{
//删除记录时的SQL语句
string delete = " DELETE From 员工信息 WHERE 姓名='";
delete += this.textBox_name.Text + "'";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = delete;
comm.Connection = m_connection;
if (m_connection.State == ConnectionState.Closed) { m_connection.Open(); }
//执行添加记录的语句
comm.ExecuteNonQuery();
//更新数据集
SetDataView();
//成功提示
MessageBox.Show("删除记录成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception error)
{
//错误提示
MessageBox.Show("删除记录发生错误:" + error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "IN button1_Click()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -