Danh mục

Practical Database Programming With Visual C#.NET- P10

Số trang: 50      Loại file: pdf      Dung lượng: 716.89 KB      Lượt xem: 14      Lượt tải: 0    
Jamona

Phí tải xuống: 18,000 VND Tải xuống file đầy đủ (50 trang) 0
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu practical database programming with visual c#.net- p10, công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Practical Database Programming With Visual C#.NET- P10 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 473DataBindings property, expand it to the Text subproperty, and perform the followingsteps to complete this binding: 1. Expand the Other Data Sources item. 2. Expand the Project Data Sources item. 3. Expand the CSE_DEPTDataSet item. 4. Expand the Faculty table. 5. Select the faculty_id. Then click on the Course ID textbox to perform the same five steps to perform anotherdata binding. The only difference between this binding and the last one is that after step 3,you need to expand the Course table and select the course_id from that table. Immediately you can find that five Design Tools have been added into this form,which are cSE_DEPTDataSet, facultyTableAdapter, facultyBindingSource, course-TableAdapter, and courseBindingSource. One point you need to note is that all ofthem are objects, not classes, and we can directly use them without needing to instantiatethem. We need to use them to complete this new course insertion query. The second method used to trigger and connect those Design Tools in our currentform window is to create instances based on the classes provided by Design Tools. To dothat, open the Insert button’s Click method from the Insert Course Form window, andenter the following codes to create three instances: CSE_DEPTDataSet cSE_DEPTDataSet = new CSE_DEPTDataSet(); CSE_DEPTDataSetTableAdapters.FacultyTableAdapter facultyTableApt = new CSE_DEPTDataSetTableAdapters.FacultyTableAdapter(); CSE_DEPTDataSetTableAdapters.CourseTableAdapter courseTableApt = new CSE_DEPTDataSetTableAdapters.CourseTableAdapter(); Now we are ready to build our new course insertion query. However, before we cando that using the TableAdapter Query Configuration Wizard, let’s first do some codingfor the project initialization and data validation.6.3.4 Project Initialization and Validate Data Before Data InsertionJust as we did for the last sample project, a data validation must be performed beforethe course information can be inserted into the database. To save time and space, we usea string array to store all course information. In total we have seven pieces of courseinformation: faculty_id, course_id, course title, course schedule, course classroom, coursecredits, and course enrollment, and all of these pieces of information should be enteredby the user into seven textboxes as the project runs. Also the combobox Faculty Nameshould be initialized by adding all faculty members to allow users to make the selectionfrom this box as the project runs, too. To do these jobs, open the constructor of the Insert Course Form, and then enterthe codes into this constructor shown in Figure 6.26.474 Chapter 6 Data Inserting with Visual C#.NET SQLInsertWizard.InsertCourseForm InsertCourseForm() public partial class InsertCourseForm : Form {A string[] CourseInfo = new string[7]; public InsertCourseForm() { InitializeComponent();B ComboName.Items.Add(Ying Bai); ComboName.Items.Add(Satish Bhalla); ComboName.Items.Add(Black Anderson); ComboName.Items.Add(Steve Johnson); ComboName.Items.Add(Jenney King); ComboName.Items.Add(Alice Brown); ComboName.Items.Add(Debby Angles); ComboName.Items.Add(Jeff Henry); ComboName.SelectedIndex = 0;C ComboMethod.Items.Add(TableAdapter Insert); ComboMethod.Items.Add(TableAdapter Update); ComboMethod.SelectedIndex = 0;D txtCourseID.DataBindings.Clear(); txtFacultyID.DataBindings.Clear(); } }Figure 6.26 Coding for the constructor of the InsertCourseForm. Let’s take a look at this piece of code to see how it works. A. First, we create a string array CourseInfo with the upper bound of 6, which means that this array contains 7 items with the index starting from 0. This array is used to store seven pieces of course information entered by the user to the seven textboxes. B. All faculty members are added into the combobox ComboName by executing the Add() method, and the first item is selected as the default faculty member. C. The first method, TableAdapter Insert, in the Combo Method box is selected as the default by setting the SelectedIndex property to 0 (the index of the combobox also starts from 0). D. These two instructions are used to clean up the binding contents in two textboxes, Faculty ID and Course ID. As you know, in order to use Design Tools to perform this new course insertion, we performed the data binding for them. One problem for those data bindings is that the bound columns would be displayed in these two textboxes as the project runs. In order to make them blank to allow users to enter new course information to these two textboxes, we prefer to clean them up. This cleaning will remove the bound relationships without affecting the Design Tools. Now let’s develop the codes for the data validation before performing the data inser-tion. This data validation makes sure that all textboxes that contain the course informa-tion are nonempty. This means that you need to fill out all textboxes with a certain pieceof course-related information and the project does not allow an empty textbox or a blankpiece of information to be inserted into the database. If you try to intentionally keepsome columns in the Course table empty, you need to place a NULL into the associatedtextbox. Open the Insert button Click method by double-clicking on the Insert button fromthe In ...

Tài liệu được xem nhiều: