Sau khi người dùng cung cấp các thông tin về khách hàng cần tạo mới và nhấn Button tạo mới ( New ), ta sẽ viết mã thực thi trong hàm bắt sự kiện nhấn nút tạo mới này. Đầu tiên ta sẽ tạo ra một dòng mới trên đối tượng DataTable, sau đó gán dữ liệu trên các TextBox cho các cột của dòng mới này :
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;
Thêm dòng mới với dữ liệu vào bảng DataTable, cập nhật vào cơ sở dữ liệu, hiển thị câu truy vấn, cập nhật DataSet, hiển thị dữ liệu mới lên hộp ListBox. Làm trắng các điều khiển TextBox bằng hàm thành viên ClearFields().
dataTable.Rows.Add(newRow); DataAdapter.Update(DataSet,"Customers"); lblMessage.Text = DataAdapter.UpdateCommand.CommandText; Application.DoEvents( ); DataSet.AcceptChanges( ); PopulateLB( ); ClearFields( );
Để hiểu rõ hoàn chỉnh ứng, ta sẽ xem mã hoàn chỉnh của toàn ứng dụng :
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace ProgrammingCSharpWindows.Form {
public class ADOForm1 : System.Windows.Forms.Form {
private System.ComponentModel.Container components; private System.Windows.Forms.Label label9;
private System.Windows.Forms.TextBox txtPhone; private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox txtContactTitle; private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtZip; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox txtCity; private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox txtAddress; private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtContactName; private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtCompanyID; private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnNew;
private System.Windows.Forms.TextBox txtCustomerName; private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Label lblMessage; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.ListBox lbCustomers; private SqlDataAdapter DataAdapter;
// biết thành viên DataSet và dataTable cho phép ta sử // dụng trên nhiều hàm khác nhau
private DataSet DataSet; private DataTable dataTable; public ADOForm1( )
{
InitializeComponent( );
string connectionString = "server=Neptune; uid=sa;" + " pwd=oWenmEany; database=northwind"; string commandString = "Select * from Customers"; DataAdapter =
new SqlDataAdapter(commandString, connectionString); DataSet = new DataSet( );
DataAdapter.Fill(DataSet,"Customers"); PopulateLB( );
}
// Đẩy dữ liệu vào điều khiển ListBox
private void PopulateLB( ) {
dataTable = DataSet.Tables[0]; lbCustomers.Items.Clear( );
foreach (DataRow dataRow in dataTable.Rows) {
lbCustomers.Items.Add( dataRow["CompanyName"] + " (" + dataRow["ContactName"] + ")" ); }
}
public override void Dispose( ) {
base.Dispose( );
components.Dispose( ); }
private void InitializeComponent( ) {
this.components = new System.ComponentModel.Container(); this.txtCustomerName=new System.Windows.Forms.TextBox(); this.txtCity = new System.Windows.Forms.TextBox();
this.txtCompanyID = new System.Windows.Forms.TextBox(); this.lblMessage = new System.Windows.Forms.Label(); this.btnUpdate = new System.Windows.Forms.Button(); this.txtContactName= new System.Windows.Forms.TextBox(); this.txtZip = new System.Windows.Forms.TextBox();