📄 controlemployees.cs
字号:
//
// labelTitle
//
this.labelTitle.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Bold);
this.labelTitle.Location = new System.Drawing.Point(8, 48);
this.labelTitle.Size = new System.Drawing.Size(44, 16);
this.labelTitle.Text = "Title:";
this.labelTitle.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// comboBoxEmployees
//
this.comboBoxEmployees.Enabled = true;
this.comboBoxEmployees.Location = new System.Drawing.Point(64, 16);
this.comboBoxEmployees.Size = new System.Drawing.Size(160, 21);
//
// ControlEmployees
//
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.labelAddress);
this.Controls.Add(this.labelEmployee);
this.Controls.Add(this.textBoxEmployeeID);
this.Controls.Add(this.labelEmployeeID);
this.Controls.Add(this.textBoxHireDate);
this.Controls.Add(this.textBoxTitle);
this.Controls.Add(this.textBoxHomePhone);
this.Controls.Add(this.labelHomePhone);
this.Controls.Add(this.textBoxPostalCode);
this.Controls.Add(this.textBoxRegion);
this.Controls.Add(this.textBoxCity);
this.Controls.Add(this.textBoxAddress);
this.Controls.Add(this.labelHireDate);
this.Controls.Add(this.labelTitle);
this.Controls.Add(this.comboBoxEmployees);
this.Size = new System.Drawing.Size(246, 302);
}
#endregion
// This function initializes or refreshes the Employees DataSet.
//
public void InitEmployees() {
// Starts the cursor icon since this function may take some time.
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
SqlCeConnection cnNorthwind = NorthwindData.GetInstance().NorthwindConnection;
DataSet dsNorthwind = NorthwindData.GetInstance().NorthwindDataSet;
DataTable dtEmployees = null;
try {
// This will execute only the first time the InitEmployees method is called.
//
if (null == daEmployees) {
daEmployees = new SqlCeDataAdapter(@"SELECT EmployeeID, LastName + ', ' + firstName AS Name, Title, HireDate, Address, City, Region, PostalCode, HomePhone FROM Employees ORDER BY Name", cnNorthwind);
daEmployees.UpdateCommand = new SqlCeCommand();
daEmployees.UpdateCommand.Connection = cnNorthwind;
// The Address, City, PostalCode, Title, HomePhone, and Region fields can be updated.
//
daEmployees.UpdateCommand.CommandText =
@"UPDATE Employees SET Address = ?, City = ?, PostalCode = ?, Title = ?, HomePhone = ?, Region = ? WHERE (EmployeeID = ?)";
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@Title", System.Data.SqlDbType.NVarChar, 30, "Title"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@HomePhone", System.Data.SqlDbType.NVarChar, 24, "HomePhone"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region"));
daEmployees.UpdateCommand.Parameters.Add(
new SqlCeParameter("@Original_EmployeeID", System.Data.SqlDbType.Int, 4, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
}
dtEmployees = dsNorthwind.Tables["Employees"];
// Initialize the Employees DataSet
//
if (null == dtEmployees) {
daEmployees.Fill(dsNorthwind, "Employees");
dtEmployees = dsNorthwind.Tables["Employees"];
// Binds the database values to the controls
//
comboBoxEmployees.DataSource = dtEmployees;
comboBoxEmployees.DisplayMember = "Name";
comboBoxEmployees.ValueMember = "EmployeeID";
textBoxEmployeeID.DataBindings.Add("Text", dtEmployees, "EmployeeID");
textBoxAddress.DataBindings.Add("Text", dtEmployees, "Address");
textBoxCity.DataBindings.Add("Text", dtEmployees, "City");
textBoxRegion.DataBindings.Add("Text", dtEmployees, "Region");
textBoxPostalCode.DataBindings.Add("Text", dtEmployees, "PostalCode");
textBoxHomePhone.DataBindings.Add("Text", dtEmployees, "HomePhone");
textBoxTitle.DataBindings.Add("Text", dtEmployees, "Title");
textBoxHireDate.DataBindings.Add("Text", dtEmployees, "HireDate");
}
// Refresh the Employees DataSet
//
else {
dtEmployees.Clear();
daEmployees.Fill(dsNorthwind, "Employees");
}
}
catch(SqlCeException e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
NorthwindData.ShowErrors(e);
this.buttonSave.Enabled = false;
return;
}
catch(Exception e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
MessageBox.Show(e.Message, "Northwind");
this.buttonSave.Enabled = false;
return;
}
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
this.buttonSave.Enabled = true;
}
// Save changes made to Employee records
//
private void buttonSave_Click(object sender, System.EventArgs e)
{
DataSet dsNorthwind = NorthwindData.GetInstance().NorthwindDataSet;
int position = BindingContext[dsNorthwind.Tables["Employees"]].Position;
dsNorthwind.Tables["Employees"].Rows[position].EndEdit();
if (MessageBox.Show("Are you sure you want to save? Pressing No will discard all changes.", "Northwind", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Asterisk, System.Windows.Forms.MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes) {
try {
daEmployees.Update(dsNorthwind, "Employees");
dsNorthwind.AcceptChanges();
}
catch(SqlCeException err) {
// Error handling mechanism
//
NorthwindData.ShowErrors(err);
}
catch(Exception err) {
// Error handling mechanism
//
MessageBox.Show(err.Message, "Northwind");
}
}
else {
try {
dsNorthwind.RejectChanges();
}
catch(SqlCeException err) {
// Error handling mechanism
//
NorthwindData.ShowErrors(err);
}
catch(Exception err) {
// Error handling mechanism
//
MessageBox.Show(err.Message, "Northwind");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -