📄 form_usermanage.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_UserManage : Form
{
private Form_Main m_FromMain=null;
private OdbcDataAdapter m_oAdapter;
private OdbcCommand m_oCMD;
public Form_UserManage(Form_Main mFrm)
{
InitializeComponent();
m_FromMain = mFrm;
m_oAdapter = new OdbcDataAdapter();
m_oCMD = new OdbcCommand();
m_oCMD.CommandText = "Select * From 用户";//ID,用户名,口令,用户类型ID,附注
m_oCMD.Connection = m_FromMain.m_connection;
m_oAdapter.SelectCommand = m_oCMD;
}
private void Form_UserManage_Load(object sender, EventArgs e)
{
try
{
// TODO: 这行代码将数据加载到表“dataSet1.用户”中。您可以根据需要移动或移除它。
//this.用户TableAdapter.Fill(this.dataSet1.用户);
// TODO: 这行代码将数据加载到表“dataSet1.用户”中。您可以根据需要移动或移除它。
GetUsers();
SetDataView();
this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
this.textBox_username.DataBindings.Add("Text", bindingSource1, "用户名");
this.textBox_pw.DataBindings.Add("Text", bindingSource1, "口令");
this.textBox_conntext.DataBindings.Add("Text", bindingSource1, "附注");
this.textBox_usertype.DataBindings.Add("Text", bindingSource1, "用户类型ID");
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_UserManage_Load()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void Form_UserManage_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
m_FromMain.m_fUM = 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[2].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_usertype.DataSource = bs;
this.comboBox_usertype.ValueMember = "类型ID";
this.comboBox_usertype.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_pw.Text != "" & this.comboBox_usertype.SelectedValue != null)
{
if (this.textBox_conntext.Text == "") { this.textBox_conntext.Text = " "; }
//更新记录的SQL语句
string update = " UPDATE 用户 SET ";
update += " 口令='";
update += this.textBox_pw.Text + "', 用户类型ID=";
update += this.comboBox_usertype.SelectedValue + ", 附注 ='";
update += this.textBox_conntext.Text + "' WHERE 用户名='";
update += this.textBox_username.Text + "'";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = update;
comm.Connection = m_FromMain.m_connection;
if (m_FromMain.m_connection.State == ConnectionState.Closed) { m_FromMain.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 bindingNavigator1_RefreshItems(object sender, EventArgs e)
{
}
private void textBox_usertype_TextChanged(object sender, EventArgs e)
{
try
{
//this.comboBox_usertype.SelectedItem = textBox_usertype.Text;
this.comboBox_usertype.SelectedValue = int.Parse(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_username.Text != "" & this.textBox_pw.Text != "" & this.comboBox_usertype.SelectedValue !=null )
{
//添加记录时的SQL命令
string insert = " INSERT INTO 用户( 用户名,口令 , 用户类型ID ,附注) VALUES ('";
insert += this.textBox_username.Text + "', '";
insert += this.textBox_pw.Text + "', ";
insert += this.comboBox_usertype.SelectedValue + ",'";
insert += this.textBox_conntext.Text + "') ";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = insert;
comm.Connection = m_FromMain.m_connection;
if (m_FromMain.m_connection.State == ConnectionState.Closed) { m_FromMain.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_username.Text = "";
this.textBox_pw.Text = "" ;
this.comboBox_usertype.SelectedValue = 1;
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_username.Text + "'";
//初始化OleDbCommand
OdbcCommand comm = new OdbcCommand();
comm.CommandText = delete;
comm.Connection = m_FromMain.m_connection;
if (m_FromMain.m_connection.State == ConnectionState.Closed) { m_FromMain.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
{
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -