📄 departmentframe.cs
字号:
new System.Data.Common.DataColumnMapping("住址", "住址")})});
this.sqlDataAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM Employee WHERE (职工号 = @Original_职工号) AND (住址 = @Original_住址 OR @Original_住址 IS NULL AND 住址 IS NULL) AND (性别 = @Original_性别 OR @Original_性别 IS NULL AND 性别 IS NULL) AND (职工名称 = @Original_职工名称 OR @Original_职工名称 IS NULL AND 职工名称 IS NULL) AND (部门号 = @Original_部门号 OR @Original_部门号 IS NULL AND 部门号 IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_职工号", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "职工号", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_住址", System.Data.SqlDbType.VarChar, 100, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "住址", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_性别", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "性别", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_职工名称", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "职工名称", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_部门号", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "部门号", System.Data.DataRowVersion.Original, null));
//
// sqlConnection
//
this.sqlConnection.ConnectionString = "workstation id=localhost;integrated security=SSPI;Database=SystemODBC;";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Employee(职工号, 职工名称, 性别, 部门号, 住址) VALUES (@职工号, @职工名称, @性别, @部门号, @住址)" +
"; SELECT 职工号, 职工名称, 性别, 部门号, 住址 FROM Employee WHERE (职工号 = @职工号)";
this.sqlInsertCommand1.Connection = this.sqlConnection;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@职工号", System.Data.SqlDbType.VarChar, 50, "职工号"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@职工名称", System.Data.SqlDbType.VarChar, 50, "职工名称"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@性别", System.Data.SqlDbType.VarChar, 10, "性别"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@部门号", System.Data.SqlDbType.VarChar, 50, "部门号"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@住址", System.Data.SqlDbType.VarChar, 100, "住址"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 职工号, 职工名称, 性别, 部门号, 住址 FROM Employee";
this.sqlSelectCommand1.Connection = this.sqlConnection;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE Employee SET 职工号 = @职工号, 职工名称 = @职工名称, 性别 = @性别, 部门号 = @部门号, 住址 = @住址 WHERE (职工号 = @Original_职工号) AND (住址 = @Original_住址 OR @Original_住址 IS NULL AND 住址 IS NULL) AND (性别 = @Original_性别 OR @Original_性别 IS NULL AND 性别 IS NULL) AND (职工名称 = @Original_职工名称 OR @Original_职工名称 IS NULL AND 职工名称 IS NULL) AND (部门号 = @Original_部门号 OR @Original_部门号 IS NULL AND 部门号 IS NULL); SELECT 职工号, 职工名称, 性别, 部门号, 住址 FROM Employee WHERE (职工号 = @职工号)";
this.sqlUpdateCommand1.Connection = this.sqlConnection;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@职工号", System.Data.SqlDbType.VarChar, 50, "职工号"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@职工名称", System.Data.SqlDbType.VarChar, 50, "职工名称"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@性别", System.Data.SqlDbType.VarChar, 10, "性别"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@部门号", System.Data.SqlDbType.VarChar, 50, "部门号"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@住址", System.Data.SqlDbType.VarChar, 100, "住址"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_职工号", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "职工号", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_住址", System.Data.SqlDbType.VarChar, 100, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "住址", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_性别", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "性别", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_职工名称", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "职工名称", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_部门号", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "部门号", System.Data.DataRowVersion.Original, null));
//
// empDataSet
//
this.empDataSet.DataSetName = "empDataSet";
this.empDataSet.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// empIDText
//
this.empIDText.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
this.empIDText.Location = new System.Drawing.Point(248, 64);
this.empIDText.Name = "empIDText";
this.empIDText.Size = new System.Drawing.Size(192, 20);
this.empIDText.TabIndex = 19;
//
// empTextID
//
this.empTextID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.empTextID.Cursor = System.Windows.Forms.Cursors.Hand;
this.empTextID.Location = new System.Drawing.Point(8, 352);
this.empTextID.Name = "empTextID";
this.empTextID.TabIndex = 20;
this.empTextID.Text = "";
//
// empNameText
//
this.empNameText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.empNameText.Cursor = System.Windows.Forms.Cursors.Hand;
this.empNameText.Location = new System.Drawing.Point(152, 352);
this.empNameText.Name = "empNameText";
this.empNameText.TabIndex = 21;
this.empNameText.Text = "";
//
// empAddress
//
this.empAddress.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.empAddress.Cursor = System.Windows.Forms.Cursors.Hand;
this.empAddress.Location = new System.Drawing.Point(440, 352);
this.empAddress.Name = "empAddress";
this.empAddress.TabIndex = 22;
this.empAddress.Text = "";
//
// DepartmentFrame
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Info;
this.ClientSize = new System.Drawing.Size(714, 391);
this.Controls.Add(this.empAddress);
this.Controls.Add(this.empNameText);
this.Controls.Add(this.empTextID);
this.Controls.Add(this.empIDText);
this.Controls.Add(this.emp_DepartNo);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.empSexText);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.dataGrid);
this.Controls.Add(this.tool);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "DepartmentFrame";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "DepartmentFrame";
this.Load += new System.EventHandler(this.DepartmentFrame_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.empDataSet)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region 窗体初始化函数
private void InitVariable()
{
try
{
#region 绑定数据库DataGrid
dsTable=this.empDataSet.Tables[0];
this.sqlDataAdapter.Fill(dsTable);
this.dataGrid.DataSource=dsTable;
#endregion
#region 绑定各个文本框上
this.empNameText.DataBindings.Add("Text",dsTable,"职工名称");
this.empSexText.DataBindings.Add("Text",dsTable,"性别");
this.empTextID.DataBindings.Add("Text",dsTable,"职工号");
this.empAddress.DataBindings.Add("Text",dsTable,"住址");
this.emp_DepartNo.DataBindings.Add("Text",dsTable,"部门号");
#endregion
#region 绑定数据表
this.orders=(System.Windows.Forms.CurrencyManager)BindingContext[dsTable];
#endregion
}
catch(Exception exel)
{
MessageBox.Show(this,exel.ToString(),"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
#region 窗体初始化...
private void DepartmentFrame_Load(object sender, System.EventArgs e)
{
InitVariable();
}
#endregion
#region ToolBar事件函数
private void Tool(System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.Text.ToString().Equals("首记录"))
{
this.dataGrid.UnSelect(this.orders.Position);
this.orders.Position=0;
this.dataGrid.Select(this.orders.Position);
this.dataGrid.CurrentRowIndex=this.orders.Position;
this.tFirst.Enabled=false;
this.tNext.Enabled=true;
this.tLast.Enabled=false;
this.tEnd.Enabled=true;
return;
}
else if(e.Button.Text.ToString().Equals("上一条记录"))
{
if(this.orders.Position.Equals(0))
{
this.tFirst.Enabled=false;
this.tLast.Enabled=false;
this.tNext.Enabled=true;
this.tEnd.Enabled=true;
}
else if(this.orders.Position>0)
{
this.dataGrid.UnSelect(this.orders.Position);
this.orders.Position--;
this.dataGrid.Select(this.orders.Position);
this.dataGrid.CurrentRowIndex=this.orders.Position;
}
return;
}
else if(e.Button.Text.ToString().Equals("下一条记录"))
{
if(this.orders.Position.Equals(this.orders.Count-1))
{
this.tFirst.Enabled=true;
this.tLast.Enabled=true;
this.tNext.Enabled=false;
this.tEnd.Enabled=false;
}
else if(this.orders.Position<this.orders.Count-1)
{
this.dataGrid.UnSelect(this.orders.Position);
this.orders.Position++;
this.dataGrid.Select(this.orders.Position);
this.dataGrid.CurrentRowIndex=this.orders.Position;
}
return;
}
else if(e.Button.Text.ToString().Equals("尾记录"))
{
this.dataGrid.UnSelect(this.orders.Position);
this.orders.Position=this.orders.Count-1;
this.dataGrid.Select(this.orders.Position);
this.dataGrid.CurrentRowIndex=this.orders.Position;
MessageBox.Show(this,"你已经到了最后一条记录,无法继续向下移啦!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.tFirst.Enabled=true;
this.tLast.Enabled=true;
this.tNext.Enabled=false;
this.tEnd.Enabled=false;
return;
}
else if(e.Button.Text.ToString().Equals("新增"))
{
this.orders.AddNew();
this.SetModify(true);
}
else if(e.Button.Text.ToString().Equals("修改"))
{
this.SetModify(true);
}
else if(e.Button.Text.ToString().Equals("删除"))
{
this.SetModify(false);
if(this.orders.Count>0)
{
this.orders.RemoveAt(this.orders.Position);
this.sqlDataAdapter.Update(this.empDataSet);
}
else
{
MessageBox.Show(this,"表已经没有数据啦!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
}
else if(e.Button.Text.ToString().Equals("保存"))
{
if(this.CheckNull().Equals(false))
{
return;
}
else
{
this.orders.EndCurrentEdit();
if(this.empDataSet.GetChanges()!=null)
{
try
{
this.sqlDataAdapter.Update(this.empDataSet);
this.SetModify(false);
}
catch(Exception ele)
{
this.empDataSet.RejectChanges();
ele.ToString();
}
}
}
return;
}
else if(e.Button.Text.ToString().Equals("取消"))
{
this.orders.CancelCurrentEdit();
this.SetModify(false);
return;
}
else if(e.Button.Text.ToString().Equals("退出"))
{
DialogResult result=MessageBox.Show(this,"是否真的退出啊!!!","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if(result.Equals(DialogResult.Yes))
{
this.Dispose();
}
else
{
return;
}
return;
}
}
#endregion
#region ToolBar事件
private void tool_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
try
{
Tool(e);
}
catch(Exception ele)
{
MessageBox.Show(this,ele.ToString(),"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
#region 非空函数
private bool CheckNull()
{
if(this.empTextID.Text.Equals(""))
{
MessageBox.Show(this,"职工号不能为空!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
return false;
}
return true;
}
#endregion
#region SetModify(bool f)
private void SetModify(bool f)
{
this.empTextID.ReadOnly=!f;
this.empNameText.ReadOnly=!f;
this.empSexText.Enabled=f;
this.emp_DepartNo.Enabled=f;
this.empAddress.ReadOnly=!f;
}
#endregion
#region 查询事件...
private void btnSearch_Click(object sender, System.EventArgs e)
{
try
{
this.sqlDataAdapter.SelectCommand.Parameters[0].Value="%";
if(!(this.empIDText.Text.Equals("")))
{
this.sqlDataAdapter.SelectCommand.Parameters[0].Value="%"+this.empIDText.Text.ToString().Trim()+"%";
}
this.empDataSet.Clear();
this.sqlDataAdapter.Fill(this.empDataSet);
this.dataGrid.DataSource=this.empDataSet.Tables[0];
}
catch(Exception ele)
{
ele.ToString();
}
}
#endregion
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -