1. Trang chủ
  2. » Công Nghệ Thông Tin

Practical Database Programming With Visual C#.NET- P10

50 565 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 716,89 KB

Nội dung

6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 473 DataBindings property, expand it to the Text subproperty, and perform the following steps 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 fi ve steps to perform another data 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 fi nd that fi ve 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 of them are objects, not classes, and we can directly use them without needing to instantiate them. 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 current form window is to create instances based on the classes provided by Design Tools. To do that, open the Insert button ’ s Click method from the Insert Course Form window, and enter 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 can do that using the TableAdapter Query Confi guration Wizard, let ’ s fi rst do some coding for the project initialization and data validation. 6.3.4 Project Initialization and Validate Data Before Data Insertion Just as we did for the last sample project, a data validation must be performed before the course information can be inserted into the database. To save time and space, we use a string array to store all course information. In total we have seven pieces of course information: faculty_id, course_id, course title, course schedule, course classroom, course credits, and course enrollment, and all of these pieces of information should be entered by the user into seven textboxes as the project runs. Also the combobox Faculty Name should be initialized by adding all faculty members to allow users to make the selection from this box as the project runs, too. To do these jobs, open the constructor of the Insert Course Form, and then enter the codes into this constructor shown in Figure 6.26 . c06.indd 473c06.indd 473 2/11/2010 11:56:05 AM2/11/2010 11:56:05 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 474 Chapter 6 Data Inserting with Visual C#.NET 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 fi rst item is selected as the default faculty member. C. T h e fi rst 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 fi ll out all textboxes with a certain piece of course - related information and the project does not allow an empty textbox or a blank piece of information to be inserted into the database. If you try to intentionally keep some columns in the Course table empty, you need to place a NULL into the associated textbox. Open the Insert button Click method by double - clicking on the Insert button from the Insert Course Form window. Enter the codes shown in Figure 6.27 into this method. public partial class InsertCourseForm : Form { string[] CourseInfo = new string[7]; public InsertCourseForm() { InitializeComponent(); 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; ComboMethod.Items.Add("TableAdapter Insert"); ComboMethod.Items.Add("TableAdapter Update"); ComboMethod.SelectedIndex = 0; txtCourseID.DataBindings.Clear(); txtFacultyID.DataBindings.Clear(); } } A B C D SQLInsertWizard.InsertCourseForm InsertCourseForm() Figure 6.26 Coding for the constructor of the InsertCourseForm. c06.indd 474c06.indd 474 2/11/2010 11:56:05 AM2/11/2010 11:56:05 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 475 The functionality of this piece of code is very simple. First, a user - defi ned method InitCourseInfo() is called to assign each textbox ’ s content to the associated string item in the string array, and then another user - defi ned method CheckCourseInfo() is executed to make sure that all textboxes are fi lled with a piece of course - related information and no empty textbox exists. The code for these two methods is shown in Figure 6.28 . The functionality of these two methods is: A. The so - called initialization of the course information assigns each textbox ’ s content to the associated string item in the string array CourseInfo. In this way, it is easier for us to check those textboxes to make sure that no textbox is empty later. B. Two local variables pos and check are created. The fi rst one is used as a loop counter for the for loop, and the second is used to store the checking result for this method. C. A for loop is utilized to scan all elements in the string array, which is equivalent to scan all textboxes, to check whether any of them is empty. The checking result check will be increased by one if this situation happened. D. Finally, the checking result is returned to the calling method. A nonzero value means that there are some empty textboxes in this Course Form window, and the value of this variable equals the number of empty textboxes checked. private void cmdInsert_Click(object sender, EventArgs e) { int check = 0; InitCourseInfo(); check = CheckCourseInfo(); } SQLInsertWizard.InsertCourseForm cmdInsert_Click() Figure 6.27 Coding for the Insert button Click method. private void InitCourseInfo() { CourseInfo[0] = txtFacultyID.Text; CourseInfo[1] = txtCourseID.Text; CourseInfo[2] = txtCourse.Text; CourseInfo[3] = txtSchedule.Text; CourseInfo[4] = txtClassRoom.Text; CourseInfo[5] = txtCredits.Text; CourseInfo[6] = txtEnroll.Text; } private int CheckCourseInfo() { int pos = 0, check = 0; for (pos = 0; pos <= 6; pos++) { if (CourseInfo[pos] == string.Empty) check++; } return check; } SQLInsertWizard.InsertCourseForm InitCourseInfo() A B C D Figure 6.28 Coding for two user - defi ned methods. c06.indd 475c06.indd 475 2/11/2010 11:56:06 AM2/11/2010 11:56:06 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 476 Chapter 6 Data Inserting with Visual C#.NET The other coding jobs for the project initialization are the coding for the Cancel and the Back command buttons. When the Back button is clicked, the current form should be closed and the project needs to be returned to the Course Form to perform the valida- tion for that new course insertion. The coding for this button is easy. Open this method and enter this.Close(); . Yes, that ’ s it! The functionality of the Cancel button is to clean up most textboxes ’ contents to allow users to reenter all course - related information into those textboxes. Open the Cancel button ’ s Click method by double - clicking on that button and enter the codes shown in Figure 6.29 into this method. The coding for this method is straightforward and easy to understand. All textboxes ’ contents, except the Course ID, are cleaned up to allow users to reenter new course information. One reason we keep the Course ID textbox ’ s contents unchanged is that this piece of information will be used later for data validation purposes since we need to retrieve the new inserted course record from the database based on the Course ID. Another reason is that we try to stop the TextChanged event from occurring to reenable the Insert button (refer to Section 6.2.8 and Figure 6.36 ). Now we can start to confi gure the TableAdapters to build our inserting data query. 6.3.5 Confi gure T able A dapter and Build Data Insertion Query Recall that when we built our sample database CSE_DEPT in Chapter 2 , there is no faculty name column in the Course table, and the only relationship that existed between the Faculty and the Course tables is the faculty_id , which is a primary key in the Faculty table but a foreign key in the Course table. As the project runs and the Insert Course Form window appears, the user needs to insert a new course record based on the faculty name, not the faculty ID. So for this new course record insertion, we need to perform two queries with two tables: First, we need to make a query to the Faculty table to get the faculty_id based on the faculty name selected by the user, and second we can insert a new course record based on the faculty_id we obtained from our fi rst query. These two queries belong to two TableAdapters: the FacultyTableAdapter and the CourseTableAdapter. Now let ’ s create these two query functions under two TableAdapters. Recall that in Section 5.14 we built a query function under the FacultyTableAdapter, FindFacultyIDByName() . In this section, we don ’ t need to redo this operation and we can use that query function directly from this project. We do need to create the second query function to insert new course records based on the selected faculty_id we obtained from the fi rst query function FindFacultyIDByName() . private void cmdCancel_Click(object sender, EventArgs e) { txtFacultyID.Text = String.Empty; txtCourse.Text = String.Empty; txtSchedule.Text = String.Empty; txtClassRoom.Text = String.Empty; txtCredits.Text = String.Empty; txtEnroll.Text = String.Empty; } SQLInsertWizard.InsertCourseForm cmdCancel_Click() Figure 6.29 Coding for the Cancel button ’ s Click method. c06.indd 476c06.indd 476 2/11/2010 11:56:06 AM2/11/2010 11:56:06 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 477 Open the Data Source window by clicking on the Data|Show Data Sources menu item from the menu bar. Then right - click on any place inside this window and select the Edit DataSet with Designer item to open the DataSet Designer Wizard. Right - click on the last line of the Course table and choose the Add|Query item to open the TableAdapter Query Confi guration Wizard. Keep the default item Use SQL statements selected and click on the Next button to go to the next window. Select the INSERT item by checking this radio button and click on Next again to continue. Click on the Query Builder button on the next window to open the Query Builder dialog box, which is shown in Figure 6.30 . The default INSERT statement is matched to our data insertion command, so we ’ d like to keep it as our query command and click on the OK button to go to the next window. On the next window, the Query Builder needs us to confi rm this query function. Two queries are displayed in this window. The fi rst one is an INSERT statement and it is what we need for our data insertion. However, the second one is the SELECT state- ment and we do not need this one for this data insertion. Therefore, highlight the second statement and delete it from this window. Click on the Next button to continue. In the next window, we need to enter the name for our query function. Enter InsertCourse into the name box and click on the Next button to go to the next window, and then click on the Finish button to complete this Query Builder dialog box and exit this process. Now we have fi nished the confi guration and the query building for the Course TableAdapter. Next, we need to develop the codes to execute this data insertion function. 6.3.6 Develop Codes to Insert Data Using T able A dapter . I nsert Method Open the Insert Course Form window and double - click on the Insert button to open its Click method, and then enter the codes shown in Figure 6.31 into this method. The Figure 6.30 Query Builder dialog box. c06.indd 477c06.indd 477 2/11/2010 11:56:06 AM2/11/2010 11:56:06 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 478 Chapter 6 Data Inserting with Visual C#.NET codes we developed in the previous section for this method have been highlighted with shading. Let ’ s take a look at this piece of code to see how it works. A. A local integer variable intInsert is created, and it is used to hold the returning status of calling the second query function InsertCourse(). B. After the methods InitCourseInfo() and CheckCourseInfo() are executed, the validation result for all textboxes is returned and stored in the local variable check . A returned value of zero means that this validation is successful and no textbox is empty. Then the ClearBeforeFill property of the TableAdapter is set to true to clean up the TableAdapter ’ s associated data table, the Faculty table in the DataSet, to make it ready to be fi lled with new records. C. T h e fi rst query function FindFacultyIDByName(), which we built in Section 5.14 , in Chapter 5 , is used to return a single data value — faculty_id — from the Faculty table based on the faculty name selected by the user from the combobox ComboName as the project runs. A string variable strFacultyID is created, and it will be used to hold the returned faculty_id obtained from the fi rst query function. D. T he returned faculty_id value is assigned to the Faculty ID textbox. This step is neces- sary. Later you will fi nd that as the project runs, only the faculty name appears and can private void cmdInsert_Click(object sender, EventArgs e) { int check = 0, intInsert = 0; InitCourseInfo(); check = CheckCourseInfo(); if (check == 0) { facultyTableAdapter.ClearBeforeFill = true; string strFacultyID = facultyTableAdapter.FindFacultyIDByName(ComboName.Text); txtFacultyID.Text = strFacultyID; if (ComboMethod.Text == "TableAdapter Insert") { intInsert = courseTableAdapter.InsertCourse(txtCourseID.Text, txtCourse.Text, txtClassRoom.Text, txtSchedule.Text, int.Parse(txtEnroll.Text), txtFacultyID.Text, double.Parse(txtCredits.Text)); } else { // insert new course using the TableAdapter.Update() method…. } if (intInsert != 0) // data insertion is successful { cmdCancel.PerformClick(); // clean up all faculty information cmdInsert.Enabled = false; // disable the Insert button } else { MessageBox.Show("The course insertion is failed"); cmdInsert.Enabled = true; } } else MessageBox.Show("Fill all Course Information box, enter a NULL for blank column"); } A B C D E F G H I SQLInsertWizard.InsertCourseForm cmdInsert_Click() Figure 6.31 Coding for the Insert button Click method. c06.indd 478c06.indd 478 2/11/2010 11:56:07 AM2/11/2010 11:56:07 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 479 be selected by the user to perform this data insertion since the user has no knowledge about the faculty_id . Therefore, when this new course insertion is performed, a NULL must be fi lled into the faculty_id textbox in order to make this data insertion operate properly. E. If the user selected the TableAdapter Insert() method to perform this data insertion, the second query function InsertCourse(), which we just built in the last section, is executed to insert new course record into the Course table in the database. Note that not only the order of inserted parameters in this InsertCourse() query function, but also the data type of each parameter, must be identical with those of the inserted parameters in the same query function InsertCourse() we built in the last section. Refer to Figure 6.30 to confi rm the order and data types for those parameters if you are not sure. Here, the Parse() method is used to convert the txtEnroll and the txtCredit from string to the integer and the double data types, respectively. F. If the user selected the TableAdapter.Update() method to perform this data insertion, the associated codes that will be developed in the next section will be executed to fi rst add the new course record into the Course table in the DataSet; and then it will be updated into the Course table in the database. G. The second query function InsertCourse() will return an integer to indicate the execution status of calling this function. Actually, the returned integer ’ s value equals the number of records that have been successfully added or inserted into the database. A returned nonzero means that this insertion is successful and a new record has been inserted into the database. If that occurred, we need to clean up the contents of all textboxes (except the Faculty Name) by executing the PerformClick() method. Also in order to avoid mul- tiple insertions for the same record, the Insert button is disabled by resetting its Enabled property to false . H. A returned zero means that no record has been inserted into the database, and this data insertion has failed. If that case occurs, a message box with a warning message is displayed. The Insert button is enabled again to allow users to perform another insertion. I. If the CheckCourseInfo() method returns a nonzero value, it means that the validation for all textboxes has failed and there are some unfi lled textboxes. A message box with a warning message is displayed to ask users to fi ll all textboxes. Now let ’ s test this data insertion by running our project. We have two ways to do it. One way is to trigger the Insert Course Form window by clicking on the Insert button from the Course Form window. To start the project in this way, we must start from the LogIn form, and then continue to the Selection Form, and to the Course Form by select- ing the Course Information item from the Selection Form window. Another way is simple. We can start the project directly from the Insert Course Form window. To start the project in this way, one needs to modify the startup object located inside the Main() method. To do that, double - click on the Program.cs item from the Solution Explorer window, and then change the startup object by replacing Application.Run(new LogInForm()); with Application.Run(new InsertCourseForm()); To start the project in the fi rst way, we need to do a little modifi cation to the Course Form. First, we need to create a fi eld - level instance of the InsertCourseForm class, c06.indd 479c06.indd 479 2/11/2010 11:56:07 AM2/11/2010 11:56:07 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 480 Chapter 6 Data Inserting with Visual C#.NET InsertCourse , and then add some codes to the Insert button ’ s Click method to display the InsertCourseForm window as the user clicks on this Insert button from the Course Form window. Open the Course Form window, create a fi eld - level instance of the class InsertCourseForm, which is shown in step A in Figure 6.32 , and then double - click on the Insert button to open its Click method. Enter the code shown in step B in Figure 6.32 into this method. As the Insert button is clicked, a new InsertCourseForm object, InsertCourse, is created and the Show() method is called to display this form window. Now let ’ s run the project using the fi rst method to test our data insertion by clicking on the Start Debugging button. The running status of the project is shown in Figure 6.33 . Enter the suitable username and password, such as jhenry and test, for the LogIn form, and select the Course Information item from the Selection Form window to open the Course Form window. Then click on the Insert button to open the Insert Course Form window. At this time, we can only test the fi rst data insertion method — TableAdapter. Insert() — since we have not developed the codes for the second method. Select a faculty namespace SQLInsertWizard { public partial class CourseForm : Form { InsertCourseForm InsertCourse = new InsertCourseForm(); ………… private void cmdInsert_Click(object sender, EventArgs e) { InsertCourse.Show(); } SQLInsertWizard.CourseForm InsertCourse A B Figure 6.32 Coding for the Insert button ’ s Click method in the Course Form. Figure 6.33 Running status of the project. c06.indd 480c06.indd 480 2/11/2010 11:56:07 AM2/11/2010 11:56:07 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 481 from the Faculty Name combobox, and enter the new course information shown in Figure 6.33 into the associated textbox. We may not remember the Faculty ID for the selected faculty; therefore, a NULL is entered at this moment (of course, you can enter the selected faculty_id to the Faculty ID textbox if you remember it). Click on the Insert button; all textboxes that contain the new course information are cleaned up except the Course ID textbox. We try to keep the content of this Course ID textbox unchanged to avoid a TextChanged event occurring, and because we want to avoid mistakenly making multiple insertions of the same data. Click on the Back , Back , and Exit buttons from the Insert Course Form, Course Form, and Selection Form windows to terminate the project. We will validate this data insertion in the next section. Next let ’ s develop the codes to insert new course data using the second method, TableAdapter.Update(). 6.3.7 Develop Codes to Insert Data Using T able A dapter . U pdate Method Open the Insert button ’ s Click method from the Insert Course Form window and add the codes shown in Figure 6.34 into this method. Since we have already developed some codes for the fi rst data insertion method in the previous section, all of those developed codes are highlighted with shading. Let ’ s take a look at this piece of code to see how it works. A. First, we need to declare a new object of the DataRow class. Each DataRow object can be mapped to a real row in a data table. Since we are using the DataSet to manage all data tables in this project, the DataSet must be prefi xed before the DataRow object. Also we need to create a row in the Course data table. The CourseRow is selected as the DataRow class. B. Next, we need to create a new object of the NewCourseRow class. C. A user - defi ned method InsertCourseRow() is called to add all information about the new inserting course, which is stored in seven textboxes, into this new created DataRow object. The functionality and the codes for this user - defi ned method is explained below. This method returns a completed DataRow in which all information about the new inserted course record has been added. D. The completed DataRow is added into the Course table in our DataSet. Note that adding a new record into the data table in the DataSet is nothing to do with adding a new record into the data table in the database. The data tables in the DataSet are only mappings of those real data tables in the database. To add this new record into the database, one needs to perform the next step. E. The TableAdapter ’ s Update() method is executed so that this new record is added into the real database. As we mentioned before, you can control the amount of data to be added into the database by passing the different arguments. Here we only want to add one new record into the Course table; therefore, a data table is passed as the argu- ment. This Update() method supposes returning an integer value to indicate whether this updating has successful or not. The value of this returned integer is equal to the number of rows that have been successfully added into the database. A returned value of zero means that this updating has failed since no new row has been added into the database. c06.indd 481c06.indd 481 2/11/2010 11:56:08 AM2/11/2010 11:56:08 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 482 Chapter 6 Data Inserting with Visual C#.NET The codes for the user - defi ned method InsertCourseRow() are shown in Figure 6.35 . Open the code window of the Insert Course Form and enter the codes shown in Figure 6.35 into this method. Let ’ s see how this piece of codes works. A. The method InsertCourseRow() needs to return a completed DataRow object, and the returned data type is indicated at the beginning of the method header after the accessing mode private . The argument is also a DataRow object, but it is a new created blank DataRow object. The data type of the argument is very important. Here we used a refer- ence mode for this argument. The advantage of using this mode is that the passed variable is an address of the DataRow object. Any modifi cation to this object, such as adding new elements to this DataRow, is permanent, and the modifi ed object can be completely returned to the calling method. B. Seven pieces of new course information stored in the associated textboxes are added into this new DataRow object, that is, added into a new row of the Course table in the DataSet. Note that the data type of each inserted item must be identical with the data type of each private void cmdInsert_Click(object sender, EventArgs e) { int check = 0, intInsert = 0; CSE_DEPTDataSet.CourseRow newCourseRow; InitCourseInfo(); check = CheckCourseInfo(); if (check == 0) { facultyTableAdapter.ClearBeforeFill = true; string strFacultyID = facultyTableAdapter.FindFacultyIDByName(ComboName.Text); txtFacultyID.Text = strFacultyID; if (ComboMethod.Text == "TableAdapter Insert") { intInsert = courseTableAdapter.InsertCourse(txtCourseID.Text, txtCourse.Text, txtClassRoom.Text, txtSchedule.Text, int.Parse(txtEnroll.Text), txtFacultyID.Text, double.Parse(txtCredits.Text)); } else { newCourseRow = cSE_DEPTDataSet.Course.NewCourseRow(); newCourseRow = InsertCourseRow(ref newCourseRow); cSE_DEPTDataSet.Course.Rows.Add(newCourseRow); intInsert = courseTableAdapter.Update(cSE_DEPTDataSet.Course); } if (intInsert != 0) // data insertion is successful { cmdCancel.PerformClick(); // clean up all faculty information cmdInsert.Enabled = false; // disable the Insert button } else { MessageBox.Show("The course insertion is failed"); cmdInsert.Enabled = true; } } else MessageBox.Show("Fill all Course Information box, enter a NULL for blank column"); } A B C D E SQLInsertWizard.InsertCourseForm cmdInsert_Click() Figure 6.34 Modifi ed coding for the Insert button ’ s Click method. c06.indd 482c06.indd 482 2/11/2010 11:56:08 AM2/11/2010 11:56:08 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... Oracle database to perform data insertion The only difference between both databases is the connection string when the Oracle database is connected to the Visual C#.NET applications To save space and time, we will not duplicate those codes in this section Refer to Section 5.2.2.2 in Chapter 5 and Appendix E to get more detailed information on how to add and connect an Oracle database with your Visual. .. will discuss how to insert data into the Oracle database using the Visual Studio.NET design tools and wizards 6.4 Insert Data into Oracle Database Using Sample Project OracleInsertWizard 489 6.4 INSERT DATA INTO ORACLE DATABASE USING SAMPLE PROJECT ORACLEINSERTWIZARD Because of the similarity in data insertion between the SQL Server database and the Oracle database, all the codes we developed in the last... Chapter 1) PART II DATA INSERTION WITH RUNTIME OBJECTS Inserting data into the database using the runtime objects method is a flexible and professional way to perform the data insertion operation in the Visual C#.NET environment Compared with those methods discussed in Part I in which Visual Studio.NET design tools and wizards were utilized to insert data into the database, the runtime objects method... records into three different databases using the runtime objects method Because of the coding similarity between these three databases, we will concentrate on inserting data to the SQL Server database using the SQLInsertRTObject project first Then we will illustrate 6.6 Insert Data into SQL Server Database Using Runtime Objects Method 491 the coding differences between these databases by using the real... codes Refer to Appendix F to get a clear picture on how to use the sample Oracle 10g XE database CSE_DEPT in C# projects As long as this connection is set up, all coding jobs are identical with those we did for the SQL Server database in the last section, and you can directly use those codes to access the Oracle database to perform the different data actions A complete data insertion project named OracleInsertWizard... validation using the Course Form window 6.3 Insert Data into SQL Server Database Using Sample Project SQLInsertWizard 485 using the stored procedures Recall that we did not discuss this for the Microsoft Access database in Section 6.2 since the Microsoft Access database does not allow users to use stored procedures to access it using the Visual Studio.NET design tools and wizards such as the TableAdapter... identical with the order in the 486 Chapter 6 Data Inserting with Visual C#.NET Figure 6.38 Opened Query Builder dialog box Figure 6.39 Confirmation dialog box InsertCourse() query function if you like However, the important issue is that it doesn’t matter if these orders are identical or not, the only key issue is that you must make sure that the order in this query function is identical with the order... allows users to insert data into the Faculty data table in the database using the runtime objects method The form also allows users to enter all pieces of information into the appropriate textboxes for the new inserted faculty By clicking the Insert button, a new faculty record is 492 Chapter 6 Data Inserting with Visual C#.NET inserted into the database However, if the user wants to reenter those pieces... used to confirm that the data insertion is successful In other words, the newly inserted data should be in the desired 494 Chapter 6 Data Inserting with Visual C#.NET table in the database and should be read back and displayed in the form window Let’s begin with the coding for the first step 6.6.3 Startup Coding and Data Validation Before Data Insertion First, let’s take care of the startup coding The... startup coding Now let’s write the coding for the data insertion 498 Chapter 6 6.6.4 Data Inserting with Visual C#.NET Insert Data into Faculty Table Since we will develop codes to perform data insertion to the SQL Server database, we need to use some classes, components, and methods related to the SQL Server database, which are defined in the System.Data.SqlClient namespace in the ADO.NET The first coding . Data Inserting with Visual C#. NET 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. Chapter 6 Data Inserting with Visual C#. NET codes we developed in the previous section for this method have been highlighted with shading. Let ’ s take

Ngày đăng: 24/10/2013, 09:15

TỪ KHÓA LIÊN QUAN