📄 returnstg.cs
字号:
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(456, 168);
this.dataGrid1.TabIndex = 6;
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Select);
//
// RetnAdapter
//
this.RetnAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.RetnAdapter.InsertCommand = this.sqlInsertCommand1;
this.RetnAdapter.SelectCommand = this.sqlSelectCommand1;
this.RetnAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "storage_retn", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("设备号", "设备号"),
new System.Data.Common.DataColumnMapping("还库日期", "还库日期"),
new System.Data.Common.DataColumnMapping("数量", "数量"),
new System.Data.Common.DataColumnMapping("经办人", "经办人"),
new System.Data.Common.DataColumnMapping("归还部门", "归还部门")})});
this.RetnAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM storage_retn WHERE (还库日期 = @Original_还库日期) AND (归还部门 = @Original_归还部门" +
" OR @Original_归还部门 IS NULL AND 归还部门 IS NULL) AND (数量 = @Original_数量) AND (经办人 = " +
"@Original_经办人 OR @Original_经办人 IS NULL AND 经办人 IS NULL) AND (设备号 = @Original_设备号" +
")";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_还库日期", System.Data.SqlDbType.DateTime, 8, 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, 30, 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, 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 storage_retn(设备号, 还库日期, 数量, 经办人, 归还部门) VALUES (@设备号, @还库日期, @数量, @经办人" +
", @归还部门); SELECT 设备号, 还库日期, 数量, 经办人, 归还部门 FROM storage_retn 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.DateTime, 8, "还库日期"));
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, "经办人"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@归还部门", System.Data.SqlDbType.VarChar, 30, "归还部门"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 设备号, 还库日期, 数量, 经办人, 归还部门 FROM storage_retn";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE storage_retn SET 设备号 = @设备号, 还库日期 = @还库日期, 数量 = @数量, 经办人 = @经办人, 归还部门 = @归还部门 WHERE (还库日期 = @Original_还库日期) AND (归还部门 = @Original_归还部门 OR @Original_归还部门 IS NULL AND 归还部门 IS NULL) AND (数量 = @Original_数量) AND (经办人 = @Original_经办人 OR @Original_经办人 IS NULL AND 经办人 IS NULL) AND (设备号 = @Original_设备号); SELECT 设备号, 还库日期, 数量, 经办人, 归还部门 FROM storage_retn 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.DateTime, 8, "还库日期"));
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("@归还部门", System.Data.SqlDbType.VarChar, 30, "归还部门"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_还库日期", System.Data.SqlDbType.DateTime, 8, 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, 30, 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, 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));
//
// ReturnStg
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 342);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.groupBox1);
this.Name = "ReturnStg";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "还库管理";
this.Load += new System.EventHandler(this.ReturnStg_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void ReturnStg_Load(object sender, System.EventArgs e)
{
MyConn.Open();//打开连接
RetnAdapter.SelectCommand.CommandText = "SELECT * FROM storage_retn";
RetnAdapter.SelectCommand.Connection = MyConn;
RetnAdapter.Fill(ReturnInfo);//填充数据集
this.dataGrid1.DataSource = ReturnInfo.DefaultView;
ReReport = (CurrencyManager) BindingContext[ReturnInfo];
DataGridTableStyle Re = new DataGridTableStyle();
this.DataBindingsFunction();
Re.AlternatingBackColor = Color.Blue;//颜色设置
Re.MappingName = ReturnInfo.TableName;
this.dataGrid1.Select(0);//选定第一列
this.textNum2.Enabled=false;
}
private void DataBindingsFunction()//绑定
{
this.textNum2.DataBindings.Add("Text",ReturnInfo,"设备号");
this.textDate.DataBindings.Add("Text",ReturnInfo,"还库日期");
this.textAmount.DataBindings.Add("Text",ReturnInfo,"数量");
this.textMan.DataBindings.Add("Text",ReturnInfo,"经办人");
this.textDep.DataBindings.Add("Text",ReturnInfo,"归还部门");
}
private void add_button_Click(object sender, System.EventArgs e)//增加记录
{
try
{
ReReport = (CurrencyManager)this.BindingContext[ReturnInfo];
ReReport.AddNew();//增加新记录
this.textNum2.Enabled=true;
this.textDate.Text=DateTime.Now.ToString();
this.change=true;//还库数量更改
this.oldtxtAmount="0";
this.textNum2.Focus();//焦点在第一个字段上
}
catch( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
private void send_button_Click(object sender, System.EventArgs e)//提交信息
{
try
{
if( MyConn.State != ConnectionState.Open)
{
MyConn.Open();
}
if(this.textNum2.Enabled==true)
{
this.textNum2.Enabled=false;
}
ReReport = (CurrencyManager)this.BindingContext[ReturnInfo];
if (this.textNum2.Text.Trim() == ""||this.textAmount.Text.Trim()=="")//检查不能为空的字段
{
MessageBox.Show("设备号,数量不能为空!");
return;
}
ReReport.EndCurrentEdit();
if(ReturnInfo.GetChanges()!=null)//信息是否被重新编辑
{
if(!change)
{
if(oldtxtAmount==this.textAmount.Text.Trim())
change=false;
else
change=true;
}
RetnAdapter.UpdateCommand.Connection=MyConn;//更新到数据库
RetnAdapter.InsertCommand.Connection=MyConn;
ReturnTran=MyConn.BeginTransaction();
RetnAdapter.UpdateCommand.Transaction = ReturnTran;
RetnAdapter.InsertCommand.Transaction = ReturnTran;
this.RetnAdapter.Update(ReturnInfo);
if(change)//还库数量字段有新值了
{
string number=this.textAmount.Text.Trim();
string driveID=this.textNum2.Text.Trim();
int changeAmt=int.Parse(number)-int.Parse(oldtxtAmount);
SqlCommand driveStorageUpdateCommand = new SqlCommand();//更新库存表的现有库存
driveStorageUpdateCommand.Connection = MyConn;//调用存储过程
driveStorageUpdateCommand.Transaction = ReturnTran;
driveStorageUpdateCommand.CommandType = CommandType.StoredProcedure;
driveStorageUpdateCommand.CommandText = "UpdateStorage";
driveStorageUpdateCommand.Parameters.Add("@i_Drive_ID",SqlDbType.Char,10);
driveStorageUpdateCommand.Parameters["@i_Drive_ID"].Value = driveID;
driveStorageUpdateCommand.Parameters.Add("@i_InOutNumber",SqlDbType.Int);
driveStorageUpdateCommand.Parameters["@i_InOutNumber"].Value = changeAmt;
driveStorageUpdateCommand.Parameters.Add("@o_ErrMessage",SqlDbType.VarChar,256);
driveStorageUpdateCommand.Parameters["@o_ErrMessage"].Direction = ParameterDirection.Output;
driveStorageUpdateCommand.ExecuteNonQuery();
ReturnTran.Commit();
MessageBox.Show("OK");
}
}
else
{
MessageBox.Show("您没有修改任何信息");
return;
}
}
catch(Exception ex)
{
MessageBox.Show("对不起,您输入的设备号错误!!具体信息:"+ex.ToString());
if( ReturnTran != null )
{
ReturnTran.Rollback();
}
if( MyConn.State != ConnectionState.Closed)
{
MyConn.Close();
}
return;
}
}
private void cancel_button_Click(object sender, System.EventArgs e)
{
try
{
ReReport = (CurrencyManager)this.BindingContext[ReturnInfo];
ReReport.CancelCurrentEdit(); //取消编辑
MessageBox.Show("已经取消!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return;
}
private void dataGrid1_Select(object sender, System.EventArgs e)//在数据集中选定记录
{
if (this.ReturnInfo.Rows.Count > 0)
{
int currentRow = this.dataGrid1.CurrentCell.RowNumber;
if (currentRow >= 0 && currentRow < ReReport.Count)
{
ReReport.Position = currentRow;
this.oldtxtAmount=this.textAmount.Text.Trim();
this.textNum2.Enabled=false;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -