?? faclitynum.cs
字號:
this.label1.Text = "設(shè)備號:";
//
// 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("設(shè)備號", "設(shè)備號"),
new System.Data.Common.DataColumnMapping("設(shè)備名稱", "設(shè)備名稱")})});
this.FacNumAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM facilityNum WHERE (設(shè)備號 = @Original_設(shè)備號) AND (設(shè)備名稱 = @Original_設(shè)備名稱 OR" +
" @Original_設(shè)備名稱 IS NULL AND 設(shè)備名稱 IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備號", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備名稱", 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(設(shè)備號, 設(shè)備名稱) VALUES (@設(shè)備號, @設(shè)備名稱); SELECT 設(shè)備號, 設(shè)備名稱 FROM fa" +
"cilityNum WHERE (設(shè)備號 = @設(shè)備號)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備號", System.Data.SqlDbType.VarChar, 10, "設(shè)備號"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, "設(shè)備名稱"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 設(shè)備號, 設(shè)備名稱 FROM facilityNum";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE facilityNum SET 設(shè)備號 = @設(shè)備號, 設(shè)備名稱 = @設(shè)備名稱 WHERE (設(shè)備號 = @Original_設(shè)備號) AND (" +
"設(shè)備名稱 = @Original_設(shè)備名稱 OR @Original_設(shè)備名稱 IS NULL AND 設(shè)備名稱 IS NULL); SELECT 設(shè)備號, 設(shè)" +
"備名稱 FROM facilityNum WHERE (設(shè)備號 = @設(shè)備號)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備號", System.Data.SqlDbType.VarChar, 10, "設(shè)備號"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, "設(shè)備名稱"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備號", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備名稱", 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 = "設(shè)備信息管理";
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);//填充數(shù)據(jù)表
this.dataGrid1.DataSource = FacNumInfo.DefaultView;
FNReport = (CurrencyManager) BindingContext[FacNumInfo];
DataGridTableStyle fn = new DataGridTableStyle();
this.DataBindingsFunction();
fn.AlternatingBackColor = Color.Blue;//顏色設(shè)置
fn.MappingName = FacNumInfo.TableName;
this.dataGrid1.Select(0);//選定第一列
}
private void DataBindingsFunction()//綁定
{
this.textNum.DataBindings.Add("Text",FacNumInfo,"設(shè)備號");
this.textName.DataBindings.Add("Text",FacNumInfo,"設(shè)備名稱");
}
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;//更新到數(shù)據(jù)庫中
FacNumAdapter.Update(FacNumInfo);
MessageBox.Show("已成功刪除");
return;
}
}
catch(Exception ex)
{
MessageBox.Show("不能刪除正在使用的數(shù)據(jù)", "提示");
}
}
private void send_button_Click(object sender, System.EventArgs e)//提交信息
{
FNReport = (CurrencyManager)this.BindingContext[FacNumInfo];
if (this.textNum.Text.Trim() == "")//檢查不能為空的字段
{
MessageBox.Show("設(shè)備號不能為空!");
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("設(shè)備號必須唯一!");
this.FacNumInfo.RejectChanges();
return;
}
}
FNReport.EndCurrentEdit();
if(FacNumInfo.GetChanges()!=null)//信息是否被重新編輯
{
try
{
FacNumAdapter.UpdateCommand.Connection=MyConn;//更新到數(shù)據(jù)庫
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("已經(jīng)取消!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return;
}
private void dataGrid1_Select(object sender, System.EventArgs e)//在數(shù)據(jù)集中選定記錄
{
if (this.FacNumInfo.Rows.Count > 0)
{
int currentRow = this.dataGrid1.CurrentCell.RowNumber;
if (currentRow >= 0 && currentRow < FNReport.Count)
FNReport.Position = currentRow;
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -