📄 faclitynum.cs
字号:
this.label1.Text = "设备号:";
//
// FacNumAdapter
//
this.FacNumAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.FacNumAdapter.InsertCommand = this.sqlInsertCommand1;
this.FacNumAdapter.SelectCommand = this.sqlSelectCommand1;
this.FacNumAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "facilityNum", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("设备号", "设备号"),
new System.Data.Common.DataColumnMapping("设备名称", "设备名称")})});
this.FacNumAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM facilityNum WHERE (设备号 = @Original_设备号) AND (设备名称 = @Original_设备名称 OR" +
" @Original_设备名称 IS NULL AND 设备名称 IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
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, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "设备名称", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=727CE26111204C4;packet size=4096;user id=sa;data source=\".\";persis" +
"t security info=False;initial catalog=storage";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO facilityNum(设备号, 设备名称) VALUES (@设备号, @设备名称); SELECT 设备号, 设备名称 FROM fa" +
"cilityNum WHERE (设备号 = @设备号)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
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, 10, "设备名称"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 设备号, 设备名称 FROM facilityNum";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE facilityNum SET 设备号 = @设备号, 设备名称 = @设备名称 WHERE (设备号 = @Original_设备号) AND (" +
"设备名称 = @Original_设备名称 OR @Original_设备名称 IS NULL AND 设备名称 IS NULL); SELECT 设备号, 设" +
"备名称 FROM facilityNum WHERE (设备号 = @设备号)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
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, 10, "设备名称"));
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, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "设备名称", System.Data.DataRowVersion.Original, null));
//
// FaclityNum
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 334);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.Name = "FaclityNum";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "设备信息管理";
this.Load += new System.EventHandler(this.FaclityNum_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void FaclityNum_Load(object sender, System.EventArgs e)
{
DataSet FacNumset=new DataSet();
MyConn.Open();//打开连接
FacNumAdapter.SelectCommand.CommandText = "SELECT * FROM facilityNum";
FacNumAdapter.SelectCommand.Connection = MyConn;
FacNumAdapter.Fill(FacNumInfo);//填充数据表
this.dataGrid1.DataSource = FacNumInfo.DefaultView;
FNReport = (CurrencyManager) BindingContext[FacNumInfo];
DataGridTableStyle fn = new DataGridTableStyle();
this.DataBindingsFunction();
fn.AlternatingBackColor = Color.Blue;//颜色设置
fn.MappingName = FacNumInfo.TableName;
this.dataGrid1.Select(0);//选定第一列
}
private void DataBindingsFunction()//绑定
{
this.textNum.DataBindings.Add("Text",FacNumInfo,"设备号");
this.textName.DataBindings.Add("Text",FacNumInfo,"设备名称");
}
private void add_button_Click(object sender, System.EventArgs e)//增加记录
{
try
{
FNReport = (CurrencyManager)this.BindingContext[FacNumInfo];
FNReport.AddNew();//增加新记录
this.textNum.Focus();//焦点在第一个字段上
}
catch( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
private void del_button_Click(object sender, System.EventArgs e)//删除操作
{
try
{
if (MessageBox.Show("您确定删除么?","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)//提示
{
FNReport = (CurrencyManager)this.BindingContext[FacNumInfo];
FNReport.RemoveAt(FNReport.Position);//删除
FacNumAdapter.DeleteCommand.Connection=MyConn;//更新到数据库中
FacNumAdapter.Update(FacNumInfo);
MessageBox.Show("已成功删除");
return;
}
}
catch(Exception ex)
{
MessageBox.Show("不能删除正在使用的数据", "提示");
}
}
private void send_button_Click(object sender, System.EventArgs e)//提交信息
{
FNReport = (CurrencyManager)this.BindingContext[FacNumInfo];
if (this.textNum.Text.Trim() == "")//检查不能为空的字段
{
MessageBox.Show("设备号不能为空!");
return;
}
for(int i=0;i<FacNumInfo.Rows.Count;i++)
{
this.numRow=FacNumInfo.Rows[i];
if (numRow[0].ToString().Trim()==this.textNum.Text.Trim())
{
MessageBox.Show("设备号必须唯一!");
this.FacNumInfo.RejectChanges();
return;
}
}
FNReport.EndCurrentEdit();
if(FacNumInfo.GetChanges()!=null)//信息是否被重新编辑
{
try
{
FacNumAdapter.UpdateCommand.Connection=MyConn;//更新到数据库
FacNumAdapter.InsertCommand.Connection=MyConn;
FacNumTran=MyConn.BeginTransaction();
FacNumAdapter.UpdateCommand.Transaction = FacNumTran;
FacNumAdapter.InsertCommand.Transaction = FacNumTran;
this.FacNumAdapter.Update(FacNumInfo);
FacNumTran.Commit();
MessageBox.Show("OK");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
}
}
else
{
MessageBox.Show("您没有修改任何信息");
return;
}
}
private void cancel_button_Click(object sender, System.EventArgs e)
{
try
{
FNReport = (CurrencyManager)this.BindingContext[FacNumInfo];
FNReport.CancelCurrentEdit(); //取消编辑
MessageBox.Show("已经取消!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return;
}
private void dataGrid1_Select(object sender, System.EventArgs e)//在数据集中选定记录
{
if (this.FacNumInfo.Rows.Count > 0)
{
int currentRow = this.dataGrid1.CurrentCell.RowNumber;
if (currentRow >= 0 && currentRow < FNReport.Count)
FNReport.Position = currentRow;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -