📄 ex-14-06
字号:
this.Controls.Add (this.txtCompanyID);
this.Controls.Add (this.label1);
this.Controls.Add (this.btnNew);
this.Controls.Add (this.txtCustomerName);
this.Controls.Add (this.btnUpdate);
this.Controls.Add (this.lblMessage);
this.Controls.Add (this.btnDelete);
this.Controls.Add (this.lbCustomers);
}
// handle the new button click
protected void btnNew_Click (object sender, System.EventArgs e)
{
// create a new row, populate it
DataRow newRow = dataTable.NewRow();
newRow["CustomerID"] = txtCompanyID.Text;
newRow["CompanyName"] = txtCompanyName.Text;
newRow["ContactName"] = txtContactName.Text;
newRow["ContactTitle"] = txtContactTitle.Text;
newRow["Address"] = txtAddress.Text;
newRow["City"] = txtCity.Text;
newRow["PostalCode"] = txtZip.Text;
newRow["Phone"] = txtPhone.Text;
// add the new row to the table
dataTable.Rows.Add(newRow);
// the following lines create an explicit insert command object
string connectionString =
"server=YourServer; uid=sa; pwd=YourPassword; database=northwind";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
connection.Open();
System.Data.SqlClient.SqlCommand insertCommand = new System.Data.SqlClient.SqlCommand();
insertCommand.CommandText = "Insert into customers values(" +
"'" + newRow["CustomerID"] + "', " +
"'" + newRow["CompanyName"] + "', " +
"'" + newRow["ContactName"] + "', " +
"'" + newRow["ContactTitle"] + "', " +
"'" + newRow["Address"] + "', " +
"'" + newRow["City"] + "', " +
"''," +
"'" + newRow["PostalCode"] + "', " +
"''," +
"'" + newRow["Phone"] + "', '')";
insertCommand.Connection = connection;
DataAdapter.InsertCommand = insertCommand;
// update the db
DataAdapter.Update(DataSet,"Customers");
// inform the user and accept the changes
lblMessage.Text = "Updated!";
Application.DoEvents();
DataSet.AcceptChanges();
// repopulate the list box
PopulateLB();
// clear all the text fields
ClearFields();
}
// set all the text fields to empty strings
private void ClearFields()
{
txtCompanyID.Text = "";
txtCompanyName.Text = "";
txtContactName.Text = "";
txtContactTitle.Text = "";
txtAddress.Text = "";
txtCity.Text = "";
txtZip.Text = "";
txtPhone.Text = "";
}
// handle the update button click
protected void btnUpdate_Click (object sender, System.EventArgs e)
{
// get the selected row
DataRow targetRow = dataTable.Rows[lbCustomers.SelectedIndex];
// inform the user
lblMessage.Text = "Updating " + targetRow["CompanyName"];
Application.DoEvents();
// edit the row
targetRow.BeginEdit();
targetRow["CompanyName"] = txtCustomerName.Text;
targetRow.EndEdit();
// get each row that changed
DataSet DataSetChanged =
DataSet.GetChanges(DataRowState.Modified);
// test to make sure all the changed rows are without errors
bool okayFlag = true;
if (DataSetChanged.HasErrors)
{
okayFlag = false;
string msg = "Error in row with customer ID ";
// examine each table in the changed DataSet
foreach (DataTable theTable in DataSetChanged.Tables)
{
// if any table has errors, find out which rows
if (theTable.HasErrors)
{
// get the rows with errors
DataRow[] errorRows = theTable.GetErrors();
// iterate through the errors and correct
// (in our case, just identify)
foreach (DataRow theRow in errorRows)
{
msg = msg + theRow["CustomerID"];
}
}
}
lblMessage.Text = msg;
}
// if we have no errors
if (okayFlag)
{
// merge the changes back into the original DataSet
DataSet.Merge(DataSetChanged);
// the following lines create an explicit update command object
string connectionString =
"server=YourServer; uid=sa; pwd=YourPassword; database=northwind";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
connection.Open();
System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand();
string updateCmd = "Update Customers set CompanyName = '" +
targetRow["CompanyName"].ToString() + "' where CustomerID = '" +
targetRow["CustomerID"].ToString() + "'";
updateCommand.CommandText = updateCmd;
updateCommand.Connection = connection;
DataAdapter.UpdateCommand = updateCommand;
// update the database
DataAdapter.Update(DataSet,"Customers");
// inform the user
lblMessage.Text = DataAdapter.UpdateCommand.CommandText;
Application.DoEvents();
// accept the changes and repopulate the list box
DataSet.AcceptChanges();
PopulateLB();
}
else // if we had errors, reject the changes
DataSet.RejectChanges();
}
// handle the delete button click
protected void btnDelete_Click (object sender, System.EventArgs e)
{
// get the selected row
DataRow targetRow = dataTable.Rows[lbCustomers.SelectedIndex];
string custID = targetRow["CustomerID"].ToString();
// prepare message for user
string msg = targetRow["CompanyName"] + " deleted. ";
// delete the selected row
dataTable.Rows[lbCustomers.SelectedIndex].Delete();
// accept the changes to the DataSet
DataSet.AcceptChanges();
// the following lines create an explicit insert command object
string connectionString =
"server=YourServer; uid=sa; pwd=YourPassword; database=northwind";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
connection.Open();
System.Data.SqlClient.SqlCommand deleteCommand = new System.Data.SqlClient.SqlCommand();
deleteCommand.CommandText = "delete from customers where customerID = " +
"'" + custID + "'";
deleteCommand.Connection = connection;
DataAdapter.DeleteCommand = deleteCommand;
// update the database
DataAdapter.Update(DataSet,"Customers");
// repopulate the list box without the deleted record
PopulateLB();
// inform the user
lblMessage.Text = msg;
Application.DoEvents();
}
public static void Main(string[] args)
{
Application.Run(new ADOForm1());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -