Practical Database Programming With Visual C#.NET- P11

50 808 0
Practical Database Programming With Visual C#.NET- P11

Đ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

6.8 Insert Data into Oracle Database Using Runtime Objects 523 Let ’ s take a look at these modifi cations to see how they work. A. Most parts of this query string work with the Oracle database, and the only modifi cations are the LIKE symbol used in the WHERE clause. Change these two LIKE symbols to the assignment operator (=:) before two parameters name and word , respectively. This is the syntax used in the Oracle database. B. Starting from step B, change the prefi x for all Oracle classes used in this method from Sql to Oracle. All modifi cations have been highlighted in bold. C. Starting from step C, change the prefi x for all Oracle objects used in this method from sql to ora. All modifi cations have been highlighted in bold. D. Modify the fi rst nominal argument @name and @word in the Add() method by removing the @ symbol before these two arguments since this is the query syntax of the Oracle database. Also change the nominal data type for these two arguments from SqlDbType to OracleType . You can perform the similar modifi cations to the codes in the ReadLogIn and the Cancel button ’ s methods. Now let ’ s go to the Faculty Form to modify the Faculty table query string. 6.8.5 Modify Faculty Query String First, make sure that the namespace System.Data.OracleClient , which contains all data components related to the Oracle database, has been added into the namespace private void cmdTabLogIn_Click(object sender, EventArgs e) { string cmdString = "SELECT user_name, pass_word, faculty_id, student_id FROM LogIn "; cmdString += "WHERE (user_name=: name ) AND (pass_word=: word)"; OracleDataAdapter LogInDataAdapter = new OracleDataAdapter(); DataTable oraDataTable = new DataTable(); OracleCommand oraCommand = new OracleCommand(); SelectionForm selForm = new SelectionForm(); oraCommand.Connection = oraConnection; oraCommand.CommandType = CommandType.Text; oraCommand.CommandText = cmdString; oraCommand.Parameters.Add("name", OracleType.Char).Value = txtUserName.Text; oraCommand.Parameters.Add("word", OracleType.Char, 8).Value = txtPassWord.Text; LogInDataAdapter.SelectCommand = oraCommand; LogInDataAdapter.Fill(oraDataTable); if (oraDataTable.Rows.Count > 0) { //MessageBox.Show("LogIn is successful"); selForm.Show(); this.Hide(); } else MessageBox.Show("No matched username/password found!"); oraDataTable.Dispose(); oraCommand.Dispose(); LogInDataAdapter.Dispose(); } A B C D OracleInsertRTObject.LogInForm cmdTabLogIn_Click() Figure 6.67 Modifi cations to the login query string in the LogIn form. c06.indd 523c06.indd 523 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 524 Chapter 6 Data Inserting with Visual C#.NET area in the code window of the Faculty Form, which is located at the top of this code window. Then open the user - defi ned UpdateFaculty() method and change the prefi x of all data classes from Sql to Oracle , and the prefi x of all data objects from sql to ora . Now open the Select button ’ s Click method by double - clicking on this button from the Faculty Form window, and perform the modifi cations shown in Figure 6.68 to this method. Let ’ s take a look at these modifi cations to see how they work. A. T h e fi rst modifi cation is to the query string. As we did in the last section, most parts of this query string work for the Oracle database, and the only modifi cation is to change the statement LIKE, which is in the WHERE clause and located before the dynamic param- eter @ name , to the Oracle assignment operator (=:) since this is the query requirement of the Oracle database. Also remove the @ symbol before the parameter name. private void cmdSelect_Click(object sender, EventArgs e) { string cmdString = "SELECT faculty_id, faculty_name, office, phone, college, title, email FROM Faculty "; cmdString += "WHERE faculty_name =: name"; OracleDataAdapter FacultyDataAdapter = new OracleDataAdapter(); OracleCommand oraCommand = new OracleCommand(); OracleDataReader oraDataReader; DataTable oraDataTable = new DataTable(); LogInForm logForm = new LogInForm(); logForm = logForm.getLogInForm(); oraCommand.Connection = logForm.oraConnection; oraCommand.CommandType = CommandType.Text; oraCommand.CommandText = cmdString; oraCommand.Parameters.Add("name", OracleType.Char).Value = ComboName.Text; string strName = ShowFaculty(ComboName.Text); if (strName == "No Match") MessageBox.Show("No Matched Faculty Image found!"); if (ComboMethod.Text == "DataAdapter Method") { FacultyDataAdapter.SelectCommand = oraCommand; FacultyDataAdapter.Fill(oraDataTable); if (oraDataTable.Rows.Count > 0) FillFacultyTable(ref oraDataTable); else MessageBox.Show("No matched faculty found!"); oraDataTable.Dispose(); FacultyDataAdapter.Dispose(); } else if (ComboMethod.Text == "DataReade r Method") { oraDataReader = oraCommand.ExecuteReader(); if (oraDataReader.HasRows == true) FillFacultyReader(oraDataReader); else MessageBox.Show("No matched faculty found!"); oraDataReader.Close(); } else MessageBox.Show("Invalid Method Selected!"); } OracleInsertRTObject.FacultyForm cmdSelect_Click() A B C D E Figure 6.68 Modifi cations to the query string in the Faculty form. c06.indd 524c06.indd 524 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.8 Insert Data into Oracle Database Using Runtime Objects 525 B. Change the prefi x of all Oracle data classes from the Sql to the Oracle , and change the prefi x of all data objects from sql to ora . Steps involved in these modifi cations are B , C , and E . All modifi cations have been indicated in bold. C. Remove the @ symbol before the fi rst argument “ name ” in the Add() method to meet the needs of the query requirement of the Oracle database. Change the prefi x of the nominal data type, which is the second argument in the Add() method, from SqlDbType to OracleType . These modifi cations are also indicated in bold. Another modifi cation is for the user - defi ned FillFacultyReader() method. The data type of the argument should be changed from SqlDataReader to OleDbDataReader . Before we can run the project to insert data into the database, we need to fi nish the rest of the modifi cations to other forms. Basically, these modifi cations change the con- nection object from SqlConnection to OleDbConnection for all other forms to match the connection requirement of the Microsoft Access database. 6.8.6 Modifi cations to Other Forms As we did for the LogIn and Faculty forms, the fi rst modifi cation we need to do is to make sure that the namespace System.Data.OracleClient has been added into the namespace area for all three forms: Selection, Course, and Insert Faculty forms. Open the code window for all of these forms and add that namespace to the namespace area if it has not been added. The second modifi cation is to modify the Connection object located at these forms. In this project we need to use the LogIn, Faculty, Selection, Course, and the Insert Faculty Form; therefore, we only need to modify the Connection object for the last three forms — Selection, Course, and the Insert Faculty Form — since we have fi nished the modifi cations for the fi rst two forms. The following methods contain this Connection object: • The Select button ’ s Click method and the Course ListBox ’ s SelectedIndexChanged method in the Course Form • The Insert button ’ s Click method in the Insert Faculty Form • The Exit button ’ s Click method in the Selection Form Open those methods and change the Connection object from sqlConnection to ora- Connection. Also perform the following modifi cations to the associated items in all forms used in this project: • Change the prefi x of all Oracle - related classes from Sql to Oracle, and the prefi x of all objects from sql to ora, respectively. • Change the data type for all passed parameters from SqlDbType to OracleType, from SqlCommand to OracleCommand, and from SqlDataReader to OracleDataReader. The third modifi cation is to change the query strings, that is, the assignment operator in the WHERE clause, from LIKE@ to the Oracle assignment operator (=:). The follow- ing methods contain the query strings: • The Course ListBox ’ s SelectedIndexChanged method in the Course Form — change “ LIKE @courseid ” to “ =: courseid ” . c06.indd 525c06.indd 525 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 526 Chapter 6 Data Inserting with Visual C#.NET • The Select button ’ Click method in the Course Form — change the joint query ON clause from “ ON (Course.faculty_id LIKE Faculty.faculty_id) AND (Faculty.faculty_name LIKE @name) ” to “ ON (Course.faculty_id = Faculty.faculty_id) AND (Faculty.faculty_name =: name) ” • The Insert button ’ s Click method in the Insert Faculty Form — change the VALUES clause from “ VALUES (@faculty_id,@faculty_name,@offi ce,@phone, @college,@title,@ email) ” to “ VALUES (:faculty_id, :faculty_name, :offi ce,:phone, :college, :title,:email ) ” . Open these methods and perform the modifi cations to the query strings. The fi nal modifi cation is for the user - defi ned InsertParameters() method in the Insert Faculty Form. Change the data type of the inserting parameters from SqlDbType to OracleType for all seven inserted parameters. Also remove the @ symbol before each parameter in the Add() method. At this point, we fi nished modifi cations to our new project OracleInsertRTObject, and you can run the project to test the data insertion to the Oracle database. A completed project OracleInsertRTObject can be found from the folder DBProjects\Chapter 6 located at the accompanying ftp site (see Chapter 1 ). In the next section, we will discuss how to insert data using the LINQ query methods. 6.9 INSERT DATA INTO DATABASE USING LINQ QUERIES As discussed in Chapter 4 , Language - Integrated Query (LINQ) is a ground - breaking innovation in Visual Studio 2008 and the .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data. In Visual Studio.NET you can write LINQ queries in C# with SQL Server databases, XML documents, ADO.NET DataSets, and any collection of objects that supports IEnumerable or the generic IEnumerable< T > interface. LINQ can be considered as a pattern or model that is supported by a collection of so - called Standard Query Operator methods we discussed in Section 4.1 , and all those Standard Query Operator methods are static methods defi ned in either IEnumerable or IQueryable classes in the namespace System.Linq . The data operated in LINQ query are object sequences with the data type of either IEnumerable < T > or IQueryable < T > , where T is the actual data type of the objects stored in the sequence. LINQ is composed of three major components: LINQ to Objects, LINQ to ADO. NET, and LINQ to XML. Where LINQ to ADO.NET contains LINQ to DataSet, LINQ to SQL, and LINQ to Entities. Because there is no LINQ to the Oracle model available, we will concentrate our discussion on inserting data into the SQL Server database using the LINQ to SQL model. Generally, the popular method to insert a new record into the database using the LINQ query follows the three steps listed below: 1. Create a new object that includes the column data to be submitted. 2. Add the new row object to the LINQ to SQL Table collection associated with the target table in the database. 3. Submit the change to the database. c06.indd 526c06.indd 526 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.10 Insert Data into Database Using Stored Procedures 527 Two ways can be used to add a new row object into the table: (1) using the Add() method and (2) using the InsertOnSubmit() method. However, both methods must be followed with the SubmitChanges() method to complete this new record insertion. In the following section, let ’ s start with the data insertion using the LINQ to SQL queries to illustrate the second method. 6.9.1 Insert Data into SQL Server Database Using LINQ to SQL Queries As discussed in Section 4.6 , to use LINQ to SQL to perform data queries, we must convert our relational database to the associated entity classes using either SQLMetal or Object Relational Designer tools. Also we need to set up a connection between our project and the database using the DataContext object. Refer to Section 4.6.1 in Chapter 4 to get a clear picture of how to create entity classes and add the DataContext object to connect to our sample database CSE_DEPT.mdf. To perform data insertion using LINQ to SQL model, refer to Sections 4.6.2 and 4.6.2.2 in Chapter 4 to get a detailed description and the coding process of a real project QueryLINQSQL, which is a Console Application, to insert a new record into the Faculty table. 6.10 INSERT DATA INTO DATABASE USING STORED PROCEDURES In this section, we discuss how to insert data into the database using stored procedures. We provided a very detailed introduction to the stored procedures in Section 5.19.2.7 in Chapter 5 and illustrated how to use this method to perform the data query for the Student form and Student table in Section 5.19.2.7.3 in Chapter 5 . Refer to that part to get more detailed descriptions about the stored procedures. We try to use the Course form and Course table to illustrate how to insert a new course record based on the selected faculty into the Course data table in this part. First, we discuss how to insert a new record into the Course table in the SQL Server database, and then we try to perform the similar function for the Oracle database. Some readers may have noted that we spent a lot of time to modify the codes in the Course form in the last project OracleInsertRTObject, but we did not use that form in that project. The reason for this issue is that we will use that Course form to illustrate inserting data into the Oracle database in the next section. 6.10.1 Insert Data into SQL Server Database Using Stored Procedures To save time and space, we can modify the project SQLInsertRTObject to create a new project named SQLInsertRTObjectSP and add one more form window to perform the data insertion using the stored procedures. Copy and paste the existing project SQLInsertRTObject to the folder C:\Chapter 6 and rename it to our new project SQLInsertRTObjectSP. Refer to Section 6.8.2 to perform the modifi cations of the project namespaces for all project fi les. Also refer to Section 6.8 to remove the SP Form and the Student Form as well as to perform the modifi cations to the Selection Form c06.indd 527c06.indd 527 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 528 Chapter 6 Data Inserting with Visual C#.NET since we do not need these two forms in this project. Recall that when we developed that project, an Insert button was added into the Course Form window. We can use this button to trigger a new form to perform the data insertion operation using the stored procedures. First, let ’ s add one more form window into this new project. The name of this new form is Insert Course Form. 6.10.1.1 Add an Inserting Data Form Window: Insert Course Form The function of this Insert Course form is: As the project runs, after the user has fi nished a correct login process and selected the item Course Information from the Selection Form, the Course Form window will be displayed. When the user clicks on the Insert button, the Insert Course Form window will appear. This form allows users to insert a new course record into the Course data table in the database using the stored procedures. The form also allows users to enter all pieces of information into the appropriate text- boxes for the new inserted course. By clicking on the Insert button, a new course record related to the selected faculty member is inserted into the database. However, if the user wants to reenter those pieces of information before fi nishing this insertion, the Cancel button can be used and all information entered will be erased. The Back button is used to allow users to return to the Course Form window to perform the validation to confi rm that the data insertion is successful. Go to the Project|Add Windows Form menu item to open the Add New Item dialog box. Keep the default Template, Windows Form, selected and enter the Insert Course Form.cs into the Name box as the name for this new form. Then click on the Add button to add this form into our project. To save time and space, we can copy all controls of this Course Form from the project SQLInsertWizard Project we developed in this chapter. Open that project from the folder DBProjects\Chapter 6 located at the accompanying ftp site (see Chapter 1 ) and open the Insert Course Form window, and then go to the Edit|Select All menu item to select all controls on that form window. Go to the Edit|Copy menu item to copy those items. Now open our project SQLInsertRTObjectSP and our new form Insert Course Form window, enlarge it to an appropriate size, and go to the Edit|Paste menu item to paste those controls into this form. One important issue to note is that the SQLInsertWizard Project is developed using the Visual Studio.NET design tools and wizards, so some objects related to those design tools and wizards such as the Data Binding Source will be added into this form as you paste those controls. Because we don ’ t need those objects in this runtime objects method, delete all of them from this new Insert Course Form window. To do that, right - click on the CourseBindingSource from the bottom of this form window and select the Delete item from the pop - up menu to remove it. In addition to removing the components related to the design tools and wizards, you also need to perform the following modifi cations to this form: • Remove the combobox control ComboMethod from this form since we only use one method, the ExecuteNonQuery method of the Command class, to execute the stored procedure to perform this data insertion. • Remove the Select button from this form since we will not perform the data validation until we click on the Back button to return to the Course Form window. In other words, the data validation is performed in the Course Form. c06.indd 528c06.indd 528 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.10 Insert Data into Database Using Stored Procedures 529 • Make sure the following properties of the form are set up: • Name: InsertCourseForm • Text: CSE DEPT Insert Course Form • AcceptButton: cmdInsert (select the Insert button as the default button) • StartPosition: CenterScreen (locate the form in the center of the screen) Your fi nished form window, Insert Course Form, should match the one shown in Figure 6.69 . The detailed descriptions of the function of each control on this form can be found in Section 6.3.2 in this chapter. Simply speaking, the Faculty Name combobox is used to allow users to select the desired faculty member to insert a new course for that selected faculty. All seven textboxes allow users to enter the information for a new course to be inserted into the Course table. The Course ID textbox is a key textbox since the Insert button will be enabled if this textbox ’ s content is changed, which means that a new course will be inserted. Generally, the Insert button should be disabled after a new course record has been inserted into the database to avoid multiple insertions of the same record. The Cancel button allows users to clean up the contents of all textboxes (except the Course ID) to reenter the course information. A new course record will be inserted into the database as the Insert button is clicked. The Back button is used to return to the Course form to perform the data validation for the new inserted course. Next let ’ s begin to develop the codes for this form. However, we need fi rst to take care of our stored procedures issue. 6.10.1.2 Develop Stored Procedures of SQL Server Database 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 exists 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 new course data based on the faculty name, not the faculty ID. Therefore, for this new course data insertion, we need to Figure 6.69 Finished Insert Course Form window. c06.indd 529c06.indd 529 2/11/2010 11:56:24 AM2/11/2010 11:56:24 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 530 Chapter 6 Data Inserting with Visual C#.NET 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 can be combined into a single stored procedure. Compared with the stored procedure, another solution to avoid performing two queries is to use a joined table query to combine these two queries together to complete a course query, as we did for the Course Form in Section 5.19.2.5 in Chapter 5 . However, it is more fl exible and convenient to use stored procedures to perform this kind of mul- tiple queries, especially when the queries are performed to multiple different data tables. Now let ’ s develop our stored procedures to combine these two queries to complete this data insertion. The stored procedure is named dbo.InsertFacultyCourse . Open Visual Studio.NET and the Server Explorer window, click on the plus symbol icon that is next to CSE_DEPT database folder to connect to our database if this database was added into the Server Explorer before. Otherwise you need to right - click on the Data Connections folder to add and connect to our database. Refer to Section 5.19.2.7.3 in Chapter 5 for the detailed information of adding and connecting the database. Right - click on the Stored Procedures folder and select the Add New Stored Procedure item to open the Add Procedure dialog box, and then enter the codes that are shown in Figure 6.70 into this new procedure. Do not forget to change the procedure ’ s name to dbo.InsertFacultyCourse , which is located at the top of this procedure. The function of this stored procedure is: A. All input parameters are listed in this part. The @FcaultyName is selected by the user from the ComboName combobox, and all other input parameters should be entered by the user to the associated textbox in the Insert Course Form window. B. A local variable @FacultyID is declared, and it is used to hold the returned value from the execution of the fi rst query to the Faculty table in step C . C. T h e fi rst query is executed to pick up the matched faculty_id from the Faculty table based on the fi rst input parameter, @FacultyName. A B C D Figure 6.70 Stored procedure dbo.InsertFacultyCourse. c06.indd 530c06.indd 530 2/11/2010 11:56:25 AM2/11/2010 11:56:25 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.10 Insert Data into Database Using Stored Procedures 531 D. The second query is used to insert a new course record into the Course table. The last parameter in the VALUES parameter list is the @FacultyID, which is obtained from the fi rst query. The coding for this stored procedure is simple and easy to be understood. One point you should know is the order of parameters in the VALUES parameter list. This order must be identical with the column order in the Course table. Otherwise, an error may be encountered when this stored procedure is saved. Go to the File|Save StoredProcedure1 menu item to save this stored procedure. Now let ’ s test this stored procedure in the Server Explorer environment to make sure that it works fi ne. Right - click on our new stored procedure dbo.InsertFacultyCourse from the Server Explorer window, and click on the Execute item from the pop - up menu to open the Run Stored Procedure dialog box. Enter the input parameters into the associated box for a new course record, and your fi nished parameters dialog box is shown in Figure 6.71 . Click on the OK button to run this stored procedure. The running result is displayed in the Output window at the bottom, which is shown in Figure 6.72 . To confi rm this data insertion, open the Course table by fi rst expanding the Tables folder in the Server Explorer window and then right - clicking on the Course folder, and Running [dbo].[InsertFacultyCourse] ( @FacultyName = Ying Bai, @CourseID = CSE- 538, @Course = Advanced Robotics, @Schedule = T-H: 1:30 - 2:45 PM, @Classroom = TC-336, @Credit = 3, @Enroll = 32 ). (1 row(s) affected) (0 row(s) returned) @RETURN_VALUE = 0 Finished running [dbo].[InsertFacultyCourse]. Output Figure 6.72 Running result of the stored procedure. Figure 6.71 Run Stored Procedure dialog box. c06.indd 531c06.indd 531 2/11/2010 11:56:25 AM2/11/2010 11:56:25 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 532 Chapter 6 Data Inserting with Visual C#.NET select the item Show Table Data . Browse the Course table until the last row, and you can fi nd that a new course, CSE - 538: Advanced Robotics , has been inserted into this table. OK, our stored procedure is successful! Next we need to develop the codes in the Visual C#.NET environment to call this stored procedure to insert a new course record into the database from our user interface. 6.10.1.3 Develop Codes to Call Stored Procedures to Insert Data into Course Table The coding for this data insertion is divided into three steps: the data validation before the data insertion, data insertion using the stored procedure, and the data validation after the data insertion. The purpose of the fi rst step is to confi rm that all inserted data that is stored in each associated textbox should be complete and valid. In other words, all textboxes should be nonempty. The third step is used to confi rm that the data inser- tion is successful, in other words, the new inserted data should be in the desired table in the database and can be read back and displayed in the Course form window. Let ’ s begin with the coding for the fi rst step. 6.10.1.3.1 Validate Data Before Data Insertion and Startup Coding First, let ’ s take care of the startup coding. The so - called startup coding includes adding the namespace related to SQL Server Data Provider, the coding for the constructor of the InsertCourseForm class, fi eld - level variables declarations, and coding for the Cancel and the Back buttons ’ Click methods. Open the code window of the Insert Course Form window and enter the codes shown in Figure 6.73 into this code window. The codes added into the constructor are used to add all faculty names into the ComboName control to allow users to select one when performing the new course insertion. Let ’ s take a look at the following pieces of codes to see how they work. A. The System.Data.SqlClient namespace is added into the namespace area since we need to use data components related to SQL Server Data Provider to perform this new course insertion. B. A fi eld - level string array CourseInfo() is created fi rst, and this array is used to store all information related to the new course to be inserted into the database. C. In the constructor of the InsertCourseForm class, 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. The user can select a desired faculty member from this com- bobox and insert a new course for that selected faculty as the project runs. D. The user - defi ned InitCourseInfo() method is used to set up a one - to - one relationship between each item in the CourseInfo string array and each textbox that contains a piece of new course information. In this way, it is easier to scan and check each textbox to make sure that none of them is empty when the user - defi ned CheckCourseInfo() method is executed later. E. To check each textbox, a for loop is utilized to scan the CourseInfo array. A warning message would be displayed, and the method returns a nonzero value to the calling method to indicate that this checking has failed if any textbox (except the Faculty ID) is empty. Otherwise a zero is returned to indicate that this checking is successful. A trick is that the for loop starts from 1, not 0, which means that this check does not include the Faculty c06.indd 532c06.indd 532 2/11/2010 11:56:26 AM2/11/2010 11:56:26 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... discussed in that part When finished with this chapter, you will: Practical Database Programming With Visual C#.NET, by Ying Bai Copyright © 2010 the Institute of Electrical and Electronics Engineers, Inc 551 552 Chapter 7 Data Updating and Deleting with Visual C#.NET • Understand the working principle and structure of updating and deleting data in the database using the Visual Studio.NET Design Tools and... Inserting with Visual C#.NET III Exercises 1 Figure 6.88 shows a stored procedure developed in the SQL Server database Please develop a piece of code in Visual C#.NET to call this stored procedure to insert a new data into the database 2 Figure 6.89 shows a piece of code developed in Visual C#.NET, and this coding is used to call a stored procedure in the Oracle database to insert a new record into the database. .. located at the folder Database\ Access located at the ftp://ftp.wiley.com/public/sci_tech_med /practical_ database site You can use any other databases such as Microsoft SQL Server 2005 SP2 or Oracle Database 10g XE The only caution is that you need to select and connect the correct database with your applications when you use the Data Source window to set up your data source for your Visual C#.NET data-driven... method, a valid database connection must be set before a new data can be inserted in the database 3 One can directly insert new data or new records into the database using the TableAdapter Update() method 4 When executing an INSERT query, the order of the input parameters in the VALUES list can be different with the order of the data columns in the database 5 To insert data into the Oracle database. .. in the database 9 To insert data into the SQL Server database using the stored procedures, one can create and test the new stored procedure in the Server Explorer window _10 To call stored procedures to insert data into a database, the parameters’ names must be identical with those names of the input parameters defined in the stored procedures II Multiple Choices 1 To insert data into the database. .. mode to the database, to insert a new record into the database, a valid must be established a DataSet b TableAdapter c Connection d Command 5 The _ operator should be used as an assignment operator for the WHERE clause with a dynamic parameter for a data query in Oracle database a =: b LIKE c = d @ 6 To confirm the stored procedure built in the Object Browser page in the Oracle database, one... procedures in the Oracle database, not package, to perform the data insertion function 540 Chapter 6 Data Inserting with Visual C#.NET As discussed in Section 5.20.3.6 in Chapter 5, different methods can be used to create Oracle’s stored procedures In this section, we will use the Object Browser page provided by Oracle Database 10g XE to create our stored procedures Open the Oracle Database 10g XE home... oraCommand.ExecuteNonQuery(); Figure 6.89 Chapter 7 Data Updating and Deleting with Visual C#.NET In this chapter, we will discuss how to update and delete data in the databases Generally, many different methods are provided and supported by Visual C#.NET and NET Framework to help users perform data updating and deleting in the database Among them, three popular methods are widely implemented: 1 Use TableAdapter... method Method 1 is developed using the Visual Studio.NET design tools and wizards, and it allows users to directly access the database and execute the TableAdapter’s methods such as TableAdapter.Insert() and TableAdapter.Update() to manipulate data in the database without requiring DataSet or DataTable objects to reconcile changes in order to send updates to a database As we mentioned at the beginning... UPDATING AND DELETING WITH VISUAL STUDIO.NET DESIGN TOOLS AND WIZARDS In this part, we discuss updating and deleting data in the database using the Visual Studio NET Design Tools and Wizards We will develop two methods to perform these data actions: First, we use the TableAdapter DBDirect methods, TableAdapter.Update() and TableAdapter.Delete(), to directly update or delete data in the database Second, . watermark. 536 Chapter 6 Data Inserting with Visual C#. NET CommandText property of the Command object to indicate to the Visual C#. NET where to fi nd this stored. to remove this watermark. 530 Chapter 6 Data Inserting with Visual C#. NET perform two queries with two tables: First, we need to make a query to the Faculty

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

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan