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

Practical Database Programming With Visual C#.NET- P12

50 632 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 1,04 MB

Nội dung

7.5 Runtime Objects Method 573 actions such as updating data and deleting data. Among them, two popular methods are widely applied: 1. Update or delete records from the desired data table in the DataSet, and then call the DataAdapter.Update() method to update the updated or deleted records from the table in the DataSet to the table in the database. 2. Build the update or delete commands using the Command object, and then call the Command ’ s method ExecuteNonQuery() to update or delete records in the database. Or you can assign the built command object to the UpdateCommand or DeleteCommand properties of the DataAdapter and call the ExecuteNonQuery() method from the UpdateCommand or DeleteCommand property. The fi rst method is to use the so - called DataSet - DataAdapter method to build a data - driven application. DataSet and DataTable classes can have different roles when they are implemented in a real application. Multiple DataTables can be embedded into a DataSet and each table can be fi lled, inserted, updated, and deleted by using the different properties of a DataAdapter such as the SelectCommand, InsertCommand, UpdateCommand, or DeleteCommand when the DataAdapter ’ s Update() method is executed. The DataAdapter will perform the associated operations based on the modi- fi cations you made for each table in the DataSet. For example, if you deleted rows from a table in the DataSet, then call this DataAdapter ’ s Update() method. This method will perform a DeleteCommand based on your modifi cations. This method is relativly simple since you do not need to call some specifi c methods such as the ExecuteNonQuery() to complete these data queries. However, this simplicity brings some limitations for your applications. For instance, you cannot access different data tables individually to perform multiple specifi c data operations. This method is very similar to the second method we discussed in Part I; therefore, we will not continue the discussion for this method. The second method allows us to use each object individually, which means that you do not have to use the DataAdapter to access the Command object or use the DataTable together with the DataSet. This provides more fl exibility. In this method, no DataAdapter or DataSet is needed, and you only need to create a new Command object with a new Connection object, and then build a query statement and attach some useful parameters into that query for the new created Command object. Then you can update or delete data against any data table by calling the ExecuteNonQuery() method, which belongs to the Command class. We will concentrate on this method in this part. In this section, we provide three sample projects named SQLUpdataDeleteRTObject, AccUpdataDeleteRTObject, and OracleUpdataDeleteRTObject to illustrate how to update or delete records in three different databases using the runtime object method. Because of the coding similarity between these three databases, we will concentrate on updating and deleting data in the SQL Server database using the sample project SQLUpdataDeleteRTObject fi rst, and then illustrate the coding differences between these databases by using the real codes for the rest of two sample projects. In addition to those three sample projects, we will also discuss data updating and deleting in our sample databases using the LINQ to SQL query method. A sample project LINQSQLUpdateDelete will be developed in this chapter to show readers how to build an actual data - driven project to update and delete data against our sample databases using the LINQ to SQL query method. c07.indd 573c07.indd 573 2/11/2010 11:57:20 AM2/11/2010 11:57:20 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 574 Chapter 7 Data Updating and Deleting with Visual C#.NET 7.6 UPDATE AND DELETE DATA FOR SQL SERVER DATABASE USING RUNTIME OBJECTS Now let ’ s fi rst develop the sample project SQLUpdataDeleteRTObject to update and delete data in the SQL Server database using the runtime objects method. Recall in Sections 5.19.2.3 to 5.19.2.7 in Chapter 5 , we discussed how to select data for the Faculty, Course, and Student Form windows using the runtime objects method. For the Faculty Form, a regular runtime selecting query is performed, and for the Course Form, a runtime joined - table selecting query is developed. For the Student table, the stored procedures are used to perform the runtime data query. Similarly in this part, we divide this discussion into two sections: 1. Update and delete data in the Faculty table from the Faculty Form window using the runtime objects method. 2. Update and delete data in the Faculty table from the Faculty Form using the runtime stored procedure method. In order to avoid duplication of the coding, we will modify an existing project named SQLInsertRTObject developed in Chapter 6 to create our new project SQLUpdata DeleteRTObject used in this section. Open Windows Explorer and create a new folder such as Chapter 7 if you have not, and then browse to the folder DBProjects\Chapter 6 located at the accompanying ftp site (See Chapter 1 ), and copy the project SQLInsertRTObject to the new folder C:\Chapter 7 we just created. Change the name of the project from SQLInsertRTObject to SQLUpdataDeleteRTObject. Double - click on the SQLUpdataDeleteRTObject.csproj to open this project. On the opened project, perform the following modifi cations to get our desired project: • Go to Project|SQLUpdataDeleteRTObject Properties menu item to open the project ’ s property window. Change the Assembly name and the Default namespace from SQLInsert RTObject to SQLUpdataDeleteRTObject, respectively. • Click on the Assembly Information button to open the Assembly Information dialog box, and change the Title and the Product to SQLUpdataDeleteRTObject. Click on the OK to close this dialog box. • Change the project namespace for all project fi les from SQLInsertRTObject to SQLUpdataDeleteRTObject using the Find and Replace dialog box. Go to the File|Save All to save those modifi cations. Now we are ready to develop our graphic user interfaces based on our new project SQLUpdataDeleteRTObject. 7.6.1 Update Data in Faculty Table for SQL Server Database Let ’ s fi rst discuss updating data in the Faculty table for the SQL Server database. To update data in the Faculty data table, we do not need to add any new window forms, and we can use the Faculty Form window as the user interface. However, we need to perform the following four steps to modify this project: c07.indd 574c07.indd 574 2/11/2010 11:57:21 AM2/11/2010 11:57:21 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.6 Update and Delete Data for SQL Server Database Using Runtime Objects 575 1. Modify the current Faculty Form window. 2. Modify the codes in the Faculty Form and the Insert Faculty Form code windows. 3. Develop codes to update data. 4. Validate the data updating. First, we need to modify the Faculty Form to make it suitable for our data updating. 7.6.1.1 Modify Faculty Form Window Recall that when we developed the Faculty Form for the project SQLInsertRTObject in the last chapter, fi ve labels were developed in that form to store the faculty information. In order to update records in the Faculty table, we need a way to enter new faculty information into some controls and update that record later. The textbox is a good can- didate to receive and store a piece of new faculty information. Therefore the fi rst job we need to do is to replace all of those fi ve labels with fi ve textbox controls and add one more textbox, Faculty Name textbox, into the Faculty Information group box since we can update a faculty member with six pieces of new information without touching the faculty_id column. A good and simple way to modify this Faculty Form window is to fi rst remove all controls from the current Faculty Form window, and then copy all controls from the Faculty Form window in the project SQLUpdateDeleteWizard we developed in this chapter, and paste them into the current Faculty Form window. To do that, fi rst remove all controls from the current Faculty Form window by clicking on the Edit|Select All , and then Edit|Delete menu items. Next open the project SQLUpdateDeleteWizard and its Faculty Form window, select Edit|Select All , and then Edit|Copy to copy all controls from that Faculty Form window. Now open our current Faculty Form window, going to the Edit|Paste menu item to paste them into our current form. Your fi nished Faculty Form window should match the one shown in Figure 7.13 . Figure 7.13 Modifi ed Faculty form window. c07.indd 575c07.indd 575 2/11/2010 11:57:21 AM2/11/2010 11:57:21 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 576 Chapter 7 Data Updating and Deleting with Visual C#.NET Note that when you perform this copy - paste operation, an object FacultyBindingSource that belongs to the project SQLUpdateDeleteWizard will also be copied and pasted into the Faculty Form. Remove this object since we do not need it in this project. 7.6.1.2 Modify Original Coding in Faculty Form The code modifi cations to the Faculty Form can be divided into two parts: (1) replace the fi eld - level label array FacultyLabel[] used to store faculty information with textbox array FacultyTextBox[] and (2) modify the codes related to those textboxes. Let ’ s begin this modifi cation from the fi rst part. Open the code window of the Faculty Form window, and change the fi eld - level label collection FacultyLabel to the textbox collection FacultyTextBox. Your fi nished textbox collection FacultyTextBox should look like: private TextBox[] FacultyTextBox = new TextBox[7]; The code modifi cation in the second part relates to codes in the three user - defi ned methods: FillFacultyTable(), MapFacultyTable(), and FillFacultyReader(). Open these methods and perform the following modifi cations shown in Figure 7.14 to these methods. All modifi ed codes have been highlighted in bold. Let ’ s take a close look at these modifi cations to see how they work. A. Replace the FacultyLabel array with the FacultyTextBox array in the user - defi ned method FillFacultyTable(). Also change the class name from Label to TextBox. Perform the same modifi cations to the codes inside the method FillFacultyReader(), which is shown in step E in Figure 7.14 . B. Change the passing - argument object in the user - defi ned method MapFacultyTable() from the label array FacultyLabel to the textbox array FacultyTextBox. Perform the same modifi cations to the codes inside the method FillFacultyReader(), which is shown in step F in Figure 7.14 . C. Change the label array FacultyLabel to the textbox array FacultyTextBox inside the foreach loop. D. Change the nominal passing - argument object in the user - defi ned method MapFacultyTable() from the label array fLabel to the textbox array fTextBox. Expand the lower bound of the FacultyTextBox array to 1 since we need to update six pieces of faculty information in our database. Also replace six label objects with six textbox objects and assign them to the associated textbox control in the Faculty Form window. E. Change the label array FacultyLabel to the textbox array FacultyTextBox inside the for loop in the FillFacultyReader() method. Note that some buttons may not respond to your clicking on them as the project runs. These buttons may include the Select, Back, and Insert buttons. This is because we delete all original controls from this Faculty Form window, and copy and paste all controls from the Faculty Form in another project SQLUpdateDeleteWizard to this Faculty Form. The relationship between each button and its delegated method is also removed with our deletion for those original controls in the Faculty Form. To fi x these errors, we can re - do these delegation relationships between each button and its method one by one. Just double - click on each button to open its delegated method and move the codes from the old method to this new one. You can also change the name of the new opened method c07.indd 576c07.indd 576 2/11/2010 11:57:21 AM2/11/2010 11:57:21 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.6 Update and Delete Data for SQL Server Database Using Runtime Objects 577 to the original one if you like. However, you need to fi rst remove the old method, and then change the name of the new method to the original one from the associated Form ’ s Designer.cs fi le by compiling and building the project. Well, quite a few modifi cations have made in this part. However, that is a good thing since we can save a lot of time when we develop the next project, OracleUpdate DeleteRTObject, by just making a few modifi cations to the current project. Now let ’ s begin to develop the codes for our data updating and deleting parts. 7.6.1.3 Develop Codes to Update Data As we mentioned in the previous sections, to update or delete an existing record from our related tables, one must follow the three steps listed in Section 7.1.1 . Open the Update button ’ s Click method by double - clicking on the Update button from the Faculty Form window and enter the codes shown in Figure 7.15 into this method. private void FillFacultyTable(ref DataTable FacultyTable) { int pos1 = 0; for (int pos2 = 0; pos2<= 6; pos2++) //Initialize the object array FacultyTextBox[pos2] = new TextBox(); // modified on 12-3-2008 MapFacultyTable(FacultyTextBox); // foreach (DataRow row in FacultyTable.Rows) { foreach (DataColumn column in FacultyTable.Columns) { FacultyTextBox[pos1].Text = row[column].ToString(); // pos1++; } } } private void MapFacultyTable(Object[] fText) // modified on 12-3-2008 { fText[1] = txtName; fText[2] = txtOffice; //The order must be identical fText[3] = txtPhone; //with the real order in the query string fText[4] = txtCollege; fText[5] = txtTitle; fText[6] = txtEmail; } private void FillFacultyReader(SqlDataReader FacultyReader) { int intIndex = 0; for (intIndex = 0; intIndex <= 6; intIndex++) //Initialize the object array FacultyTextBox[intIn dex] = new TextBox(); MapFacultyTable(FacultyTextBox); while (FacultyReader.Read()) { for (intIndex = 0; intIndex <= FacultyReader.FieldCount - 1; intIndex++) FacultyTextBox[intIndex].Text = FacultyReader.GetString(intIndex); } } A B C D E F G SQLUpdateDeleteRTObject.FacultyForm FillFacultyReader() Figure 7.14 Modifi cations to the Faculty Form. c07.indd 577c07.indd 577 2/11/2010 11:57:21 AM2/11/2010 11:57:21 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 578 Chapter 7 Data Updating and Deleting with Visual C#.NET Let ’ s take a look at this piece of code to see how it works. A. The Update query string is defi ned fi rst at the beginning of this method. All six data columns in the Faculty table are input parameters. The dynamic parameter @Param1 represents the old faculty name, which is the faculty name that has not been updated. B. All data components and local variables are declared here such as the Command object, LogInForm object, and intUpdate . The LogInForm object and the getLogInForm() method are used to access the Connection object we built in the LogIn Form object and initialize the Command object with the Connection object below. The integer variable intUpdate is used to hold the returned data from calling the ExecuteNonQuery() method. C. The Command object is initialized and built using the Connection object and the Parameter object. D. A user - defi ned method UpdateParameters() is called to add all updating parameters into the Command Parameters ’ property. The passing mode used for the passed argument is passing by reference, which means that a valid starting address of that Command object is passed into the method, and any modifi cation to this Command object is permanent and it can be returned to the calling method. E. Then the ExecuteNonQuery() method of the Command class is executed to update the faculty table. The running result of this method is returned and stored in the local variable intUpdate . private void cmdUpdate_Click(object sender, EventArgs e) { string cmdString = "UPDATE Faculty SET faculty_name = @name, office = @office, phone = @phone, " + "college = @college, title = @title, email = @email “ + “WHERE (faculty_name LIKE @Param1)"; LogInForm logForm = new LogInForm(); logForm = logForm.getLogInForm(); SqlCommand sqlCommand = new SqlCommand(); int intUpdate = 0; sqlCommand.Connection = logForm.sqlConnection; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = cmdString; UpdateParameters(ref sqlCommand); intUpdate = sqlCommand.ExecuteNonQuery(); sqlCommand.Dispose(); ComboName.Items.Clear(); UpdateFaculty(); if (intUpdate == 0) MessageBox.Show("The data updating is failed"); } private void UpdateParameters(ref SqlCommand cmd) { cmd.Parameters.Add("@name", SqlDbType.Char).Value = txtName.Text; cmd.Parameters.Add("@office", SqlDbType.Char).Value = txtOffice.Text; cmd.Parameters.Add("@phone", SqlDbType.Char).Value = txtPhone.Text; cmd.Parameters.Add("@college", SqlDbType.Char).Value = txtCollege.Text; cmd.Parameters.Add("@title", SqlDbType.Char).Value = txtTitle.Text; cmd.Parameters.Add("@email", SqlDbType.Char).Value = txtEmail.Text; cmd.Parameters.Add("@Param1", SqlDbType.Char).Value = ComboName.Text; } A B C D E SQLUpdateDeleteRTObject.FacultyForm cmdUpdate_Click() F G H Figure 7.15 Coding for the data updating operation. c07.indd 578c07.indd 578 2/11/2010 11:57:21 AM2/11/2010 11:57:21 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.6 Update and Delete Data for SQL Server Database Using Runtime Objects 579 F. The Command object is released after this data updating, and the updated faculty members are refreshed in the ComboName box by executing the UpdateFaculty() method. Before this updating can be refreshed, the ComboName box is cleaned up by running its Clear() method. G. The returned value from calling the ExecuteNonQuery() method is equal to the number of rows that have been updated in the Faculty table. A zero means that no row has been updated, an error message is displayed, and the procedure is exited if this situation occurred. H. The detailed coding for the user - defi ned method UpdateParameters() is shown in this step. Six pieces of new faculty information are assigned to the associated columns in the Faculty table. At this point, we have fi nished the coding for the data updating operation for the Faculty table. Next let ’ s take care of the data validation after this data updating to confi rm that our data updating is successful. 7.6.1.4 Validate Data Updating We do not need to add any new form windows to perform this data validation, and we can use the Faculty Form window to perform this validation operation. By clicking on the Select button on the Faculty Form window, we can perform the selection query to retrieve the updated faculty record from the database and display it on the Faculty Form. Before we can run the project to test the data updating function, we prefer to fi rst complete the coding for the data deleting operation. 7.6.2 Delete Data from Faculty Table for SQL Server Database As we mentioned in the previous section, to delete a faculty record from our database, we have to follow the two steps listed below: 1. First, delete records from the child tables (LogIn and Course tables). 2. Second, delete records from the parent table (Faculty table). The data deleting function can be performed by using the Delete button ’ s Click method in the Faculty Form window. Therefore, the main coding for this functionality is developed inside that method. 7.6.2.1 Develop Codes to Delete Data Open the Delete button ’ s Click method by double - clicking on the Delete button from the Faculty Form window, and enter the codes shown in Figure 7.16 into this method. Let ’ s take a close look at this piece of code to see how it works. A. The deleting query string is declared fi rst at the beginning of this method. The only input parameter is the faculty_name . Although the primary key of the Faculty table is faculty_id , in order to make it convenient to the user, the faculty_name is used as the criterion for this data deleting query. A potential problem of using the faculty_ name column as the deleting criterion is that no duplicated faculty_name should exist in the Faculty table for this application. In other words, each faculty name must be unique c07.indd 579c07.indd 579 2/11/2010 11:57:22 AM2/11/2010 11:57:22 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 580 Chapter 7 Data Updating and Deleting with Visual C#.NET in the Faculty table. A solution to this potential problem is that we can use the faculty_ id as the criterion for the data deleting query in the future. B. A MessageBox button ’ s object is created, and this object is used to display both buttons in the MessageBox, Yes and No, when the project runs. C. All data components and local variables used in this method are declared here, too. The data type of the variable Answer is DialogResult. However, one can use an integer vari- able to replace it. The LogInForm object and the getLogInForm() method are used to access the Connection object we built in the LogIn Form object and initialize the Command object with that Connection object below. The integer variable intDelete is used to hold the returned data from calling the ExecuteNonQuery() method to delete a record from the Faculty table. D. As the Delete button is clicked when the project runs, fi rst a MessageBox is displayed to confi rm that the user wants to delete the selected member from the Faculty table. E. If the user ’ s answer to the MessageBox is Yes, then the deleting operation begins to be processed. The Command object is initialized and built by using the Connection object and the Command string we defi ned at the beginning of this procedure. F. The dynamic parameter @Param1 is replaced by the real parameter, the faculty name stored in the combobox ComboName. A key point to note is that you must use the faculty name stored in the combobox control, which is an existing faculty name. However, you cannot use the faculty name stored in the Faculty Name textbox since that is an updating faculty name. G. The ExecuteNonQuery() method of the Command class is called to execute the data delet- ing query to the Faculty table. The running result of calling this method is stored in the local variable intDelete . private void cmdDelete_Click(object sender, EventArgs e) { string cmdString = "DELETE FROM Faculty WHERE (faculty_name LIKE @Param1)"; MessageBoxButtons vbButton = MessageBoxButtons.YesNo; LogInForm logForm = new LogInForm(); logForm = logForm.getLogInForm(); SqlCommand sqlCommand = new SqlCommand(); DialogResult Answer; int intDelete = 0; Answer = MessageBox.Show("Do you want to delete this record?", "Delete", vbButton); if (Answer == System.Windows.Forms.DialogResult.Yes) { sqlCommand.Connection = logForm.sqlConnection; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = cmdString; sqlCommand.Parameters.Add("@Param1", SqlDbType.Char).Value = ComboName.Text; intDelete = sqlCommand.ExecuteNonQuery(); sqlCommand.Dispose(); if (intDelete == 0) MessageBox.Show("The data Deleting is failed"); for (intDelete = 0; intDelete < 7; intDelete++) // clean up the Faculty textbox array FacultyTextBox[intDelete].Text = string.Empty; } } A B C D E UpdateDeleteRTObject.FacultyForm cmdDelete_Click() SQL F G H I J Figure 7.16 Coding for the data deleting query. c07.indd 580c07.indd 580 2/11/2010 11:57:22 AM2/11/2010 11:57:22 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.6 Update and Delete Data for SQL Server Database Using Runtime Objects 581 H. The Command object is released after the data deleting. I. The returned value from calling of the ExecuteNonQuery() method is equal to the number of rows that have been successfully deleted from the Faculty table. If a zero returns, which means that no row has been deleted from the Faculty table and this data deleting has failed, an error message is displayed and the method is exited if that situation occurred. J. After the data deleting is done, all faculty information stored in the six textboxes should be cleaned up. A for loop is used to fi nish this cleaning job. Finally let ’ s take care of the coding to validate the data deleting query. 7.6.2.2 Validate Data Updating and Deleting As we did for the validation of the data updating in the last section, we do not need to create any new form window to do this validation, and we can use the Faculty Form to perform this data validation. Now let ’ s run the project to test both data updating and data deleting operations. Before we can run the project, make sure that a default faculty photo fi le named Default. jpg has been stored in the default folder in our project if an updating photo is involved in this data updating. In this application, this default folder is the folder in which the executable fi le of our Visual C#.NET project is located, which is C:\Chapter 7\ SQLUpdateDeleteRTObject\bin\Debug . Click on the Start Debugging button to start our project, enter the suitable username and password to the LogIn form, and select the item Faculty Information from the Selection Form to open the Faculty Form window. First, let ’ s select a faculty member and retrieve all information related to the selected faculty from our database, and display those pieces of information in this form. Then we can update this faculty by modifying some pieces of information stored in related textboxes. In this test, we select Ying Bai as a faculty member and enter the following updated information to the related textboxes: • Professor Title textbox • MTC - 305 Offi ce textbox • 750 - 378 - 1127 Phone textbox Click on the Update button in the Faculty Form window to update this record. To validate this data updating, fi rst select another faculty from the combobox control ComboName and click on the Select button to retrieve all information for that faculty. Then go to the combobox again and select our updated faculty name Ying Bai from the box, and click on the Select button to retrieve back the updated information for that selected faculty member. Immediately you can fi nd that all pieces of updated information related to the selected faculty are displayed in this form. This means that our data updat- ing is successful. Your updated faculty information window should match the one shown in Figure 7.17 . Next let ’ s test the data deleting function by clicking on the Delete button to try to delete this updated faculty record from the Faculty table. Click on Yes to the message box and all updated faculty information stored in six textboxes is gone. Is our data delet- ing successful? To answer this question, click on the Select button again to try to retrieve that deleted faculty information from the Faculty table. What happens after you click on c07.indd 581c07.indd 581 2/11/2010 11:57:22 AM2/11/2010 11:57:22 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 582 Chapter 7 Data Updating and Deleting with Visual C#.NET the Select button? A message “ No matched faculty found ” shows up, and this means that the selected faculty and all information related to that selected faculty have been successfully deleted from the Faculty table. Yes, our data deleting is successful. To keep our sample database neat and complete, it is highly recommended to recover those updated and deleted faculty records after this testing. To perform this recovering work successfully, the following points must be kept in mind: 1. Recall that when we built our sample database, that is, when we set up the relationships among tables, we selected the Cascade mode for both Update and Delete Rules for INSERT and UPDATE Specifi cation fi elds between the Faculty and LogIn, Faculty and Course, and Course and StudentCourse tables. This means that among these tables, the Faculty is a parent table for the LogIn and the Course tables and the LogIn and the Course are child tables to the Faculty table. Similarly, the Course is a parent table for the StudentCourse table, and the StudentCourse is a child table to the Course table. The cascade updating and deleting means that when a record in the parent table is updated or deleted, all related records in the child tables are also updated or deleted. An example of cascade updating and deleting is shown in Figure 7.18 . The faculty member Ying Bai with a faculty_id=B78880 is selected for this cascade updating and deleting example. 2. As shown in Figure 7.18 , if the faculty member with a faculty_id=B78880 , which is a primary key in the Faculty table but a foreign key in the LogIn and the Course tables, is updated or deleted from the Faculty table, the related records in the child tables, LogIn and Course, with the same faculty_id will also be updated or deleted with the associated columns. Two associated columns, user_name and pass_word , located in the LogIn table and four associated columns, course_id , located in the Course table will also be updated or deleted. Similarly, if a course_id that is a primary key in the Course table but a foreign key in the StudentCourse table is updated or deleted from the Course table, all related records in the child table, StudentCourse, will also be updated or deleted. Figure 7.18 shows the associated columns that will be affected when these cascade updating or deleting actions are performed for the selected faculty member Ying Bai . 3. An important issue is the order to recover these deleted records. You have to fi rst recover the records in the parent table (Faculty and Course tables) and then recover the records in Figure 7.17 Updated faculty information window. c07.indd 582c07.indd 582 2/11/2010 11:57:22 AM2/11/2010 11:57:22 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... UPDATE AND DELETE DATA FOR ORACLE DATABASES USING RUNTIME OBJECTS Data Updating and Deleting with Visual C#.NET Because of the coding similarity between the SQL Server and the Oracle databases for the data updating and deleting, in this section we only show the differences in the coding for both databases The main differences between the SQL Server and the Oracle databases are the syntax in the query... parts based on three types of databases we used in this book: using stored procedures to update and delete data in (1) Microsoft Access 2007 database, (2) SQL Server database, and (3) Oracle database To save time and space, we will not duplicate any project, and we want to modify some existing projects to create our desired projects 7.8.1 Update and Delete Data in Access Database Using Stored Procedures... first close the Visual Studio.NET to disconnect the connection between our project and our sample database, and then reopen our project and the Server Explorer to perform that deleting confirmation Otherwise the deleted data cannot be reflected in our sample database Our stored procedure is successful In order to keep our database neat, we prefer to recover these deleted faculty records with the original... to insert a new faculty record with a new faculty_id, which is a common way used in most real data-driven applications First, let’s create the updating stored procedure to update one faculty record Open our sample database CSE_DEPT.accdb, which can be found from the folder Database\ Access located at the accompanying ftp site (see Chapter 1) You had better copy that database and paste it in a folder... point, we have finished creating our data updating and deleting stored procedures in Microsoft Access 2007 database Close our sample database by exiting the Access 2007 Now let’s develop the codes in Visual C#.NET to call these stored procedures to perform the data updating and deleting actions against the database 7.8.1.3 Call Stored Procedure to Update Faculty Information Open the Faculty Form and its Update... procedure—dbo.UpdateFacultySP 2 The names of the input parameters—identical with the columns’ names in the Faculty table in our sample database 3 The name of the input dynamic parameter—@FacultyName 4 The name of the data deleting stored procedure—dbo.DeleteFacultySP 5 The names of the input parameters—identical with the columns’ names in the Faculty table in our sample database 6 The name of the input dynamic parameter—@FacultyName... identical with those parameters we used in this method Now we have finished all coding modifications in the Visual C#.NET environment Let’s start to create our stored procedures in the SQL Server database There are two ways you can create a stored procedure: one way is to create it in the SQL Server Management Studio Express, and the other way is to create it in the Server Explorer in the Visual Studio.NET... Microsoft Access 2007 database 7.8.1.2 Create Stored Procedures in Microsoft Access Database As we mentioned at the beginning of this section, the data updating operation updates all columns of one existing faculty record except the faculty_id column since it is 7.8 Update and Delete Data in Database Using Stored Procedures Figure 7.20 593 Stored procedure in Microsoft Access database unnecessary to... Oracle database To develop our new project in this section, we need to: 1 Modify the existing project OracleUpdateDeleteRTObject to create our new project OracleUpdateDeleteSP 2 Develop stored procedures in the Oracle database 3 Call the stored procedures to perform the data updating and deleting using the Faculty Form window Now let’s start with the first step 7.8 Update and Delete Data in Database. .. faculty named Ying Bai has been replaced by our updated record, which is shown in Figure 7.21 594 Chapter 7 Figure 7.21 Data Updating and Deleting with Visual C#.NET Confirmation of the data updating Now recover this record to the original one for this faculty with the following information since we want to keep our data unchanged: • Ying Bai faculty_name column • MTC-211 office column • 750-378-1148 phone . watermark. 574 Chapter 7 Data Updating and Deleting with Visual C#. NET 7.6 UPDATE AND DELETE DATA FOR SQL SERVER DATABASE USING RUNTIME OBJECTS Now let ’ s fi. watermark. 584 Chapter 7 Data Updating and Deleting with Visual C#. NET 7.7 UPDATE AND DELETE DATA FOR ORACLE DATABASES USING RUNTIME OBJECTS Because of the

Ngày đăng: 28/10/2013, 16:15

TỪ KHÓA LIÊN QUAN