Programming C# 2nd Edition phần 7

các trường trong bảng Khách hàng trong cơ sở dữ liệu Northwind. Ngoài ra còn có một nhãn (lblMessage) mà bạn có thể sử dụng để viết tin nhắn cho người sử dụng (hiện nay báo chí mới, Update, hoặc Xóa). Truy cập các dữ liệu đầu tiên, tạo ra các đối tượng DataAdapter và DataSet như các biến thành viên tư nhân, cùng với các DataTable: | Programming C 2nd Edition Figure 14-7. The ADO update form This form consists of a list box ibCustomers a button for Update btnUpdate an associated text box txtCustomerName and a Delete button btnDelete . There is also a set of eight text fields that are used in conjunction with the New button btnNew . These text fields represent eight of the fields in the Customers table in the Northwind database. There is also a label ibiMessage that you can use for writing messages to the user it currently says Press New Update or Delete . Accessing the Data First create the DataAdapter object and the DataSet as private member variables along with the DataTable private SqlDataAdapter dataAdapter private DataSet dataSet private DataTabie dataTabie This enables you to refer to these objects from various member methods. Start by creating strings for the connection and the command that will get you the table you need string connectionString server local NetSDK Trusted Connection yes database northwind string commandString Select from Customers These strings are passed as parameters to the SqiDataAdapter constructor dataAdapter new SqlDataAdapter commandString connectionString A DataAdapter may have four SQL commands associated with it. Right now we have only one . The InitializeCommands method creates the remaining three InsertCommand UpdateCommand and DeleteCommand. InitializeCommands uses the AddParms method to associate a column in each SQL command with the columns in the modified rows 349 Programming C 2nd Edition private void AddParms SqlCommand cmd params string cols Add each parameter foreach String column in cols @ column 0 column InitializeCommands creates each SQL command in turn using placeholders that correspond to the column argument passed to AddParm private void InitializeCommands Reuse the SelectCommand s Connection. SqlConnection connection SqlConnection Create an

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.