Practical Database Programming With Visual C#.NET- P9

50 537 0
Practical Database Programming With Visual C#.NET- P9

Đ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

5.20 Query Data Using Runtime Objects to Oracle Database 423 The prototype of the procedure SelectFacultyCourse() is declared in line 3. Two arguments are used for this procedure: input parameter FacultyName, which is indicated as an input by using the keyword IN followed by the data type of VARCHAR2. The output parameter is a cursor named FacultyCourse followed by the keyword OUT. Each PL - SQL statement must end with a semicolon, and this rule also applies to the END statement. Figure 5.175 Opened Create Package window. Figure 5.176 Name page of the Package window. c05.indd 423c05.indd 423 2/11/2010 2:59:14 PM2/11/2010 2:59:14 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 424 Chapter 5 Data Selection Query with Visual C#.NET Click on the Finish button to complete this step. You can click on the Compile button to compile this specifi cation block if you like. Next we need to create the body block of this package. Click on the Body tab to open the Body page, which is shown in Figure 5.179 . Click on the Edit button to begin to create our body part. Enter the PL - SQL codes into this body shown in Figure 5.180 . The procedure prototype is redeclared in line 2. But an IS operator is attached at the end of this prototype and it is used to replace the AS operator to indicate that this proce- Figure 5.177 Opened Specifi cation page. Figure 5.178 Coding for the Specifi cation page. c05.indd 424c05.indd 424 2/11/2010 2:59:18 PM2/11/2010 2:59:18 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.20 Query Data Using Runtime Objects to Oracle Database 425 dure needs to use a local variable facultyId, and this variable will work as an intermediate variable to hold the returned faculty_id from the fi rst query, which is located at line 6. Starting from BEGIN, our real SQL statements are included in lines 6 and 7. The fi rst query is to get the faculty_id from the Faculty table based on the input parameter FacultyName, which is the fi rst argument of this procedure. A SELECT … INTO state- ment is utilized to temporarily store the returned faculty_id into the intermediate variable facultyId. Figure 5.179 Opened Body page of the package. Figure 5.180 Coding for the Body part of the package. c05.indd 425c05.indd 425 2/11/2010 2:59:21 PM2/11/2010 2:59:21 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 426 Chapter 5 Data Selection Query with Visual C#.NET The OPEN FacultyCourse FOR command is used to assign the returned data columns from the following query to the cursor variable FacultyCourse. Recall that we used a SET command to perform this assignment functionality in the SQL Server stored procedure in Section 5.19.2.7.4 . Starting from lines 9 and 10, the second query is declared, and it is to get all course_id and courses taught by the selected faculty from the Course table based on the intermediate variable ’ s value, faculty_id, which is obtained from the fi rst query above. The queried results are assigned to the cursor variable FacultyCourse. Now let ’ s compile our package by clicking on the Compile button. A successful compiling information PL/SQL code successfully compiled (22:20:06) will be displayed if this package is bug free, which is shown in Figure 5.181 . The development of our Oracle package is complete, and now let ’ s go to the Visual Studio.NET to call this package to perform our course query for our Course form. 5.20.3.9 Query Data Using Oracle Package for Course Form Open the Course form window and double - click on the Select button to open its Click method and enter the codes shown in Figure 5.182 into this method. Let ’ s take a look at this piece of code to see how it works. A. The package query string is declared, and this string contains both the package ’ s name (Faculty_Course) and the stored procedure ’ s name (Select FacultyCourse). All query strings for the Oracle database package must follow this style. The package ’ s name and the procedure ’ s name defi ned in this string must be identical with those names we used when we created this package in the Object Browser in Oracle Database 10g XE. Otherwise your calling to this package would fail. Figure 5.181 Compiled coding for the body part of the package. c05.indd 426c05.indd 426 2/11/2010 2:59:26 PM2/11/2010 2:59:26 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.20 Query Data Using Runtime Objects to Oracle Database 427 private void cmdSelect_Click(object sender, EventArgs e) { string cmdString = "Faculty_Course.SelectFacultyCourse"; OracleParameter paramFacultyName = new OracleParameter(); OracleParameter paramFacultyCourse = new OracleParameter(); OracleDataAdapter CourseDataAdapter = new OracleDataAdapter(); OracleCommand oraCommand = new OracleCommand(); DataTable oraDataTable = new DataTable(); OracleDataReader oraDataReader; LogInForm logForm = new LogInForm(); logForm = logForm.getLogInForm(); paramFacultyName.ParameterName = "FacultyName"; paramFacultyName.OracleType = OracleType.VarChar; paramFacultyName.Value = ComboName.Text; paramFacultyCourse.ParameterName = "FacultyCourse"; paramFacultyCourse.OracleType = OracleType.Cursor; paramFacultyCourse.Direction = ParameterDirection.Output; oraCommand.Connection = logForm.oraConnection; oraCommand.CommandType = CommandType.StoredProcedure; oraCommand.CommandText = cmdString; oraCommand.Parameters.Add(paramFacultyName); oraCommand.Parameters.Add(paramFacultyCourse); if (ComboMethod.Text == "DataAdapter Method") { CourseDataAdapter.SelectCommand = oraCommand; CourseDataAdapter.Fill(oraDataTable); if (oraDataTable.Rows.Count > 0) FillCourseTable(oraDataTable); else MessageBox.Show("No matched course found!"); oraDataTable.Dispose(); CourseDataAdapter.Dispose(); } else if (ComboMethod.Text == "DataReader Method" ) { oraDataReader = oraCommand.ExecuteReader(); if (oraDataReader.HasRows == true) FillCourseReader(oraDataReader); else MessageBox.Show("No matched course found!"); oraDataReader.Close(); } else //LINQ to DataSe t Method is selected . { CourseDataAdapter.SelectCommand = oraCommand; CourseDataAdapter.Fill(ds, "Course"); var courseinfo = (from ci in ds.Tables["Course"].AsEnumerable() select ci); CourseList.Items.Clear(); foreach (var cRow in courseinfo) { CourseList.Items.Add(cRow.Field<string>("course_id")); } ds.Clear(); } CourseList.SelectedIndex = 0; } A B C D E F G H I J K L M N O P Q OracleSelectRTObject.CourseForm cmdSelect_Click() Figure 5.182 Coding for the Select button Click method. c05.indd 427c05.indd 427 2/11/2010 2:59:27 PM2/11/2010 2:59:27 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 428 Chapter 5 Data Selection Query with Visual C#.NET B. All data components used to perform this query are declared and created here. First, two Oracle Parameter objects are created: paramFacultyName and paramFacultyCourse. These two Parameter objects will be passed into the calling package, and they work as input and output parameters, respectively. Some other components, such as the DataAdapter, Command, DataTable, and Data Reader, are also created here. C. T h e fi rst Parameter object is initialized here. Both parameter name, FacultyName, and the data type, VARCHAR, must be identical with the name and the data type we used when we created this procedure in Oracle Database 10g XE R2. The parameter ’ s value should be equal to the selected name from the faculty name combobox (ComboName.Text) in the Course form window in Visual C#.NET. D. The second parameter is also initialized with the associated parameter name and data type. One important point is that the second parameter is an output parameter and its data type is cursor, and the transmission direction of this parameter is output. So the Direction property of this Parameter object must be clearly indicated by assigning the Output to it. Otherwise, the procedure calling may encounter some error and this error is hard to debug. E. The Command object is initialized by assigning the associated property, such as the Connection, CommandType, and CommandText. The CommandType must be StoredProcedure and the CommandText should be the query string we declared at the beginning of this method (step A ). F. Two initialized Parameter objects are added into the Command object, that is, they are added into the Parameters collection property of the Command object. G. If the user selected the DataAdapter method, the initialized Command object is assigned to the SelectCommand property of the DataAdapter, and the Fill() method is executed to fi ll the Course table. Basically, only at this moment, the Oracle package we developed is called and two queries are executed. The returned columns should be stored in the Course data table if this fi ll is successful. H. If the Count property of the Course table is greater than 0, which means that at least one row is fi lled into the table, the user - defi ned method FillCourseTable() is called to fi ll the queried course_id into the CourseList box in the Course form window. Otherwise, an error message is displayed to indicate that this fi ll has failed. I. Some cleaning jobs are performed to release some data objects used for this query. J. If the user selected the DataReader method, the ExecuteReader() method is executed to invoke the DataReader to retrieve required columns and store the returned results into the DataReader. If the property HasRows is true, which means that DataReader did read back some rows, the user - defi ned method FillCourseReader() is called to fi ll the CourseList box in the Course form window with the read rows. Otherwise, an error message is displayed. K. Another cleaning job is performed to release the DataReader used for this query. L. If the user selected the LINQ to DataSet method, the initialized Command object is assigned to the SelectCommand property of the DataAdapter, and the Fill() method is executed to initialize the DataSet with the stored procedure. This operation will not only fi ll the Course table but also the Faculty table since there are two query operations in the package we built above. M. A typical LINQ query structure is created and executed to retrieve back all course_id from the Course table. The courseinfo is a Visual C# 2008 implicitly typed local variable with a data type var. The Visual C# 2008 will be able to automatically convert this var to any suitable data type. In this case, it is a collection, when it sees it. An iteration variable ci is used to iterate over the result of this query from the Course table. Then a similar SQL c05.indd 428c05.indd 428 2/11/2010 2:59:27 PM2/11/2010 2:59:27 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.20 Query Data Using Runtime Objects to Oracle Database 429 SELECT statement is executed without the WHERE clause. The reason for that is because we have built the Command object oraCommand by assigning both the input and output parameters to the Parameters collection. Therefore, we do not need to use any other query criterion here. Another key point for this structure is the operator AsEnumerable(). Since different database systems use different collections and query operators, those collections must be converted to a type of IEnumerable < T > in order to use the LINQ technique because all data operations in LINQ use Standard Query Operators that can perform complex data queries on an IEnumerable < T > sequence. A compiling error would be encountered without this operator. N. The CourseList box is cleaned up to make it ready to add and display all course_id for the selected faculty. This code is important and necessary. Without this line, duplicated course_id will be added and displayed in this CourseList box. O. A foreach loop is utilized to pick up each course_id from the selected result cRow, which is obtained from the courseinfo we get from the LINQ query. Then, add each course_id into the CourseList box in the CourseForm window to display them. Since we are using a nontyped DataSet, we must indicate the column clearly with the fi eld < string > and the column ’ s name as the position for each of them. P. The DataSet is cleaned up after this query. This operation is important and necessary. Without this line, multiple duplicated course_ids would be added and displayed in the CourseList box since multiple duplicated query results are fi lled into the DataSet, that is, into the Course table. Q. This statement is very important and it is used to select the fi rst course_id in the CourseList box as the default course as the Course form is opened. More important, this command can work as a trigger event to trigger the CourseList box ’ s SelectedIndexChanged method to display the detailed information related to that default course_id. The coding for that method is our next job. The coding for the FillCourseTable() and the Back button Click method have nothing to do with any object used in this project. Thus, no coding modifi cation is needed. The user - defi ned method FillCourseReader() needs only one small modifi cation, which is to change the nominal argument ’ s type to OracleDataReader since now we are using an Oracle data provider. The detailed explanations for methods FillCourseTable() and FillCourseReader() can be found in Figure 5.92 . For your convenience, we list this piece of code with some explanations again, which is shown in Figure 5.183 . Let ’ s see how this piece of code works. A. Before we can fi ll the CourseList box, a cleaning job is needed. This cleaning is very important. Otherwise multiple repeat course_ids would be added and displayed in this listbox if you forget to clean it up fi rst. B. A foreach loop is used to scan all rows of the fi lled Course table. Recall that we fi lled 6 columns of data from the Course table in the database to this Course table in the DataTable object, starting with the fi rst column — course_id — and the second column — course. Now we need to pick up the fi rst column — course_id (column index = 0) — for each returned row or record of the Course table. Then the Add() method of the ListBox control is used to add each retrieved course_id into the CourseList box. C. For the FillCourseReader() method, the data type of the passed argument is Oracle DataReader since we are using an Oracle database as our data source. D. A local string variable strCourse is created, and this variable can be considered as an intermediate variable used to temporarily hold the queried data from the Course table. c05.indd 429c05.indd 429 2/11/2010 2:59:28 PM2/11/2010 2:59:28 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 430 Chapter 5 Data Selection Query with Visual C#.NET E. Similarly, we need to clean up the CourseList box before it can be fi lled. F. A while loop is utilized to retrieve each fi rst column ’ s data [GetString(0)] whose column index is 0 and the data value is the course_id. The queried data fi rst is assigned to the intermediate variable strCourse, and then it is added into the CourseList box by using the Add() method. Next we need to take care of the coding for the CourseList_SelectedIndexChanged() method. The functionality of the coding for this method is to display the detailed course information, such as the course title, course credit, classroom, course schedule, and enroll- ment, for the selected course_id by the user when the user clicks on a course_id from the CourseList box. Five textbox controls in the Course form are used to store and display the detailed course information. Now let ’ s begin to do our coding for this method. Open the Course form window and double - click on the CourseList box (any place inside that list box) to open this method. Enter the codes shown in Figure 5.184 into this method. Only the DataAdapter query method is used for this method. Let ’ s take a look at this piece of code and see how it works. A. The query string is fi rst declared, and we need to retrieve six columns from the Course table. In fact, we have already gotten the course_id from the last query in the Select button method. However, in order to keep the code neat, we still retrieve this column from this query. A nominal parameter courseid that works as a dynamic parameter is assigned to the course_id column as our query criterion. You need to note that the assignment opera- tor for the dynamic parameter in Oracle is an equal operator plus a colon. B. All data components used to perform this query are declared here, such as the DataAdapter, Command object, and the DataTable objects. The keyword Oracle needs to be prefi xed before these objects since we are using the Oracle data components to perform this query. The new instance of the LogInForm class is used to allow us to access and use the global connection object defi ned in that class. C. The Command object is initialized by assigning it with associated properties such as the Connection, CommandType, and CommandText. private void FillCourseTable(DataTable CourseTable) { CourseList.Items.Clear(); foreach (DataRow row in CourseTable.Rows) { CourseList.Items.Add(row[0]); //the 1st column is course_id - cmdString } } private void FillCourseReader(OracleDataReader CourseReader) { string strCourse = string.Empty; CourseList.Items.Clear(); while (CourseReader.Read()) { strCourse = CourseReader.GetString(0); //the 1st column is course_id CourseList.Items.Add(strCourse); } } A B C D E F OracleSelectRTObject.CourseForm FillCourseTable() Figure 5.183 Coding for the FillCourseTable and FillCourseReader methods. c05.indd 430c05.indd 430 2/11/2010 2:59:28 PM2/11/2010 2:59:28 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.20 Query Data Using Runtime Objects to Oracle Database 431 D. The dynamic parameter courseid is added into the Parameters collection, which is a prop- erty of the Command object using the Add() method. The real value of this parameter is the course_id that is selected by the user from the CourseList box. E. The SelectCommand property of the DataAdapter is assigned with the initialized Command object, and the Fill() method is executed to fi ll the course table. F. If the Count property of the returned data table is greater than 0, which means that at least one row is fi lled into the table, the FillCourseTextBox() method is called to fi ll fi ve textbox controls with retrieved six columns. Otherwise, an error message is displayed if this fi ll has failed. G. Some cleaning job is performed here to release all objects used for this fi ll table operation. Two user - defi ned methods, FillCourseTextBox() and MapCourseTable(), have no relationship with any object used in this project. Therefore, no coding modifi cation is needed for them. For the detailed line - by - line explanations of these two methods, refer to Figure 5.94 in section 5.18.4 . The coding for the Back button Click method is easy. Open this method and enter this.Hide(); into this method. That ’ s all! Before we can run the project to test our codes, we need to copy all faculty image fi les to the folder in which our Visual C# executable fi le is located. In this application, it is the Debug folder of the project. In our case, this folder is located at C:\Book 6\Chapter 5\OracleSelectRTObject\bin\Debug. Now let ’ s start this project to test the codes we developed for the Course form. Click on the Debug| Start Debugging button to run the project. Enter the suitable username and password, such as jhenry and test for the LogIn form, and then select the Course Information item from the Selection form window to open the Course form. Select the desired faculty private void CourseList_SelectedIndexChanged(object sender, EventArgs e) { string cmdString = "SELECT course, credit, classroom, schedule, enrollment, course_id FROM Course "; cmdString += "WHERE course_id =:courseid"; OracleDataAdapter CourseDataAdapter = new OracleDataAdapter(); OracleCommand oraCommand = new OracleCommand(); 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("courseid", OracleType.Char).Value = CourseList.SelectedItem; CourseDataAdapter.SelectCommand = oraCommand; CourseDataAdapter.Fill(oraDataTable); if (oraDataTable.Rows.Count > 0) FillCourseTextBox(oraDataTable); else MessageBox.Show("No matched course information found!"); oraDataTable.Dispose(); oraCommand.Dispose(); CourseDataAdapter.Dispose(); } A B C D E F G OracleSelectRTObject.CourseForm CourseList_SelectedIndexChanged() Figure 5.184 Coding for the SelectedIndexChanged method. c05.indd 431c05.indd 431 2/11/2010 2:59:28 PM2/11/2010 2:59:28 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 432 Chapter 5 Data Selection Query with Visual C#.NET name from the Faculty Name combobox and click on the Select button to list all courses taught by this faculty in the CourseList box. Then click each course_id item from the CourseList box, and the detailed course information related to the selected course_id will be displayed in fi ve textbox controls in this form, which is shown in Figure 5.185 . You can try to perform this query by using different methods with different faculty members. Our coding is successful! At this point we fi nished all coding for this project. As for the coding for the Student form, we prefer to leave this job to students as their homework. A complete project named XEOracleSelectRTObject can be found in the folder DBProjects\Chapter 5 that is located at the accompanying ftp site (see Chapter 1 ). 5.21 CHAPTER SUMMARY The main topic of this chapter is to develop professional data - driven applications in the Visual C#.NET 2008 environment by using different methods. The data query is the main topic of this chapter. The fi rst method is to utilize Design Tools and Wizards provided by Visual Studio. NET 2008 and ADO.NET to build simple but powerful data query projects. The second method is to use the runtime objects method to build the portable data query projects. The third method is to use LINQ to DataSet and LINQ to SQL to simplify the data query and improve query effi ciency. Comparably, the fi rst method is simple, and it is easy to be understood and learned by those students who are beginners to Visual C# and databases. This method utilizes many powerful tools and wizards provided by Visual Studio.NET 2008 and ADO.NET to simplify the coding process, and most of codes are autogenerated by the .NET Framework 3.5 and Visual C#.NET 2008 as the user uses those tools and wizards to perform data operations such as adding new data source, making data binding, and con- Figure 5.185 Running status of the Course form. c05.indd 432c05.indd 432 2/11/2010 2:59:28 PM2/11/2010 2:59:28 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... principle and structure of inserting data to the database using the runtime objects method Practical Database Programming With Visual C#.NET, by Ying Bai Copyright © 2010 the Institute of Electrical and Electronics Engineers, Inc 439 440 Chapter 6 Data Inserting with Visual C#.NET • Insert data into the DataSet using LINQ to DataSet and insert data into the database using LINQ to SQL queries • Design and... use any other databases such as Microsoft SQL Server 2005 or Oracle Database 10 g XE The only issue is that you need to select and connect to the correct database when you use the Data Source window to set up your data source for your Visual C#.NET data-driven applications 6.1 INSERT NEW DATA INTO A DATABASE Generally, there are many different ways to insert new data into the database in Visual C#.NET... methods • Use the OleDbCommand, SqlCommand, and OracleCommand class to execute the data query with dynamic parameters to three kinds of databases • Use the OleDbDataAdapter to fill a DataSet and a DataTable object with three kinds of databases • Use the OleDbDataReader class to query and process data with three kinds of databases • Use LINQ to DataSet to simplify the data query process and improve the data... DATA INSERTING WITH VISUAL STUDIO.NET DESIGN TOOLS AND WIZARDS In this part, we discuss inserting data into the database using the Visual Studio.NET design tools and wizards We develop two methods to perform this data insertion: First, we use the TableAdapter DBDirect method, TableAdapter.Insert(), to directly insert data into the database Second, we show readers how to insert data into the database by... DataSet to the database using the TableAdapter.Update() method Both methods utilize the TableAdapter’s direct and indirect methods to complete the data insertion The database we use is the Microsoft Access 2007 database, CSE_DEPT.accdb, which was developed in Chapter 2 and is located in the folder Database\ Access located at the accompanying site: ftp://ftp.wiley.com/public/sci_tech_med /practical_ database. .. query from Visual C#.NET • Use the SQL nested stored procedure to perform the data query from Visual C#.NET • Use the Object Browser in Oracle Database 10g XE to create, debug, and test stored procedures and packages • Use the Oracle stored procedures and packages to perform the data query from Visual C#.NET In Chapter 6, we will discuss the data-inserting technique with three kinds of databases Three... Command object combined with the ExecuteNonQuery() method When using method 1, one can directly access the database and execute commands such as TableAdapter.Insert(), TableAdapter.Update(), and TableAdapter.Delete() to 6.1 Insert New Data into a Database 441 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... them in the next section 6.2.4 Duplicate Visual C#.NET Projects with Installed DataSet Unlike Visual Basic.NET projects, in which one can create a new project by duplicating an existing project and then make some simple modifications on that duplicated project, in Visual C#.NET, this duplication is not as easy as that in Visual Basic.NET The main issue is that in Visual C#.NET, the namespace is widely... Access 2007, SQL Server 2005 Express, and Oracle 10g XE databases • Use the OleDbCommand, SqlCommand, and OracleCommand class to dynamically execute the data query with dynamic parameters to three kinds of databases • Use the OleDbDataAdapter, SqlDataAdapter, and OracleDataAdapter to dynamically fill a DataSet and a DataTable object with three kinds of databases • Use the OleDbDataReader, SqlDataReader,... statement in the SQL Server database, an @ symbol must be prefixed before the nominal variable _16 To access different databases by using the LINQ technique, different LINQ APIs must used For example, to access the SQL Server database, LINQ to SQL is used, to access the Oracle database, LINQ to Oracle must be used _17 To assign a dynamic parameter in a SELECT statement in the SQL Server database, the keyword . remove this watermark. Chapter 6 Data Inserting with Visual C #. NET Practical Database Programming With Visual C#. NET, by Ying Bai Copyright © 2010 the Institute. query from Visual C#. NET. • Use the SQL nested stored procedure to perform the data query from Visual C#. NET. • Use the Object Browser in Oracle Database

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

Từ khóa liên quan

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

Tài liệu liên quan