Practical Database Programming With Visual C#.NET- P8

50 507 0
Practical Database Programming With Visual C#.NET- P8

Đ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.19 Query Data Using Runtime Objects to SQL Server Database 373 C. The FindName() function is executed to get the student ’ s photo fi le based on the student ’ s name. The returned student ’ s image fi le is assigned to the local string variable strName. D. The user - defi ned method BuildCommand() is called to initialize the fi rst Command object with the correct Connection, CommandType, and CommandText properties. In order to execute our stored procedure, the properties should be set as follows: • CommandType = CommandType. StoredProcedure • CommandText = “ dbo.StudentInfo ” Figure 5.129 Testing result for our second stored procedure. One point you need to note is that if you are using SQL Server Management Studio Express to build your database, in some situations, you cannot connect the server to open the database if you are performing some tasks with the Server Explorer such as creating stored procedures because your server has been connected and the database is open when you create stored procedures. An error message would be displayed if you try to do that since this version only allows one instance of the server to be connected at a time. You have to disconnect that connection fi rst by rebooting your computer. The content of the CommandText must be equal to the name of the stored procedure we developed above. E. The unique input parameter to the stored procedure dbo.StudentInfo is StudentName, which will be selected by the user from the student name combobox (ComboName.Text) as the project runs. This dynamic parameter must be added into the Parameters collection c05.indd 373c05.indd 373 2/11/2010 2:58:54 PM2/11/2010 2:58:54 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 374 Chapter 5 Data Selection Query with Visual C#.NET private void cmdSelect_Click(object sender, EventArgs e) { string strStudent = "dbo.StudentInfo"; string strStudentCourse = "dbo.StudentCourseInfo"; SqlDataAdapter StudentDataAdapter = new SqlDataAdapter(); SqlDataAdapter StudentCourseDataAdapter = new SqlDataAdapter(); SqlCommand sqlCmdStudent = new SqlCommand(); SqlCommand sqlCmdStudentCourse = new SqlCommand(); DataTable sqlStudentTable = new DataTable(); DataTable sqlStudentCourseTable = new DataTable(); SqlDataReader sqlStudentReader, sqlStudentCourseReader; string strName = string.Empty; strName = FindName(ComboName.Text); if (strName == "No Match") MessageBox.Show("No Matched Student's Image Found!"); BuildCommand(ref sqlCmdStudent, strStudent); sqlCmdStudent.Parameters.Add("@StudentName", SqlDbType.Char).Value = ComboName.Text; StudentDataAdapter.SelectCommand = sqlCmdStudent; if (ComboMethod.Text == "DataAdapter Method") { StudentDataAdapter.Fill(sqlStudentTable); if (sqlStudentTable.Rows.Count > 0) FillStudentTextBox(sqlStudentTable); else MessageBox .Show(" No matched student found!"); BuildCommand(ref sqlCmdStudentCourse, strStudentCourse); sqlCmdStudentCourse.Parameters.Add("@StudentID", SqlDbType.Char).Value = txtID.Text; StudentCourseDataAdapter.SelectCommand = sqlCmdStudentCourse; StudentCourseDataAdapter.Fill(sqlStudentCourseTable); if (sqlStudentCourseTable.Rows.Count > 0) FillCourseList(sqlStudentCourseTable); else MessageBox.Show("No matched course_id found!"); } else //DataReader Method is selected { sqlStudentReader = sqlCmdStudent.ExecuteReader(); if (sqlStudentReader.HasRows == true) FillStudentReader(sqlStudentReader); else MessageBox.Show("No matched student found!"); BuildCommand(ref sqlCmdStudentCourse, strStudentCourse); sqlCmdStudentCourse.Parameters.Add("@StudentID", SqlDbType.Char).Value = txtID.Text; StudentCourseDataAdapter.SelectCommand = sqlCmdStudentCourse; sqlStudentCourseReader = sqlCmdStudentCourse.ExecuteReader(); if (sqlStudentCourseReader.HasRows == true) FillCourseReader(sqlStudentCourseReader); else MessageBox.Show("No matched course_id found!"); sqlStudentReader.Close(); sqlStudentCourseReader.Close(); } sqlStudentTable.Dispose(); sqlStudentCourseTable.Dispose(); StudentDataAdapter.Dispose(); StudentCourseDataAdapter.Dispose(); sqlCmdStudent.Dispose(); sqlCmdStudentCourse.Dispose(); } A B C D E F G H I J K L M N O P Q R S SQLSelectRTObject.StudentForm cmdSelect_Click() Figure 5.130 Coding for the Select button Click method. c05.indd 374c05.indd 374 2/11/2010 2:58:55 PM2/11/2010 2:58:55 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.19 Query Data Using Runtime Objects to SQL Server Database 375 that is the property of the Command class by using the Add() method before the stored procedure can be executed. The initialized Command object sqlCmdStudent is assigned to the SelectCommand property of the DataAdapter to make it ready to be used in the next step. F. If the user selected the DataAdapter Method, the Fill() method of the DataAdapter is called to fi ll the Student table, which actually calls our fi rst stored procedure to fi ll the Student table. G. If this calling is successful, the Count property should be greater than 0, which means that at least one row is fi lled into the Student table, and the user - defi ned method FillStudentTextBox() is called to fi ll six textboxes in the Student form with six pieces of retrieved columns from the stored procedure. Otherwise, an error message is displayed if this fi ll has failed. H. The user - defi ned method BuildCommand() is called again to initialize our second Command object sqlCmdStudentCourse. The values to be assigned to the properties of the Command object are: • CommandType = CommandType. StoredProcedure • CommandText = “ dbo.StudentCourseInfo ” The content of the CommandText must be equal to the name of the stored procedure we developed above. I. The unique input parameter to the stored procedure dbo.StudentCourseInfo is the StudentID, which is obtained from the calling of the fi rst stored procedure and stored in the student ID textbox txtID. This dynamic parameter must be added into the Parameters collection that is the property of the Command class by using the Add() method before the stored procedure can be executed. The initialized Command object sqlCmdStudentCourse is assigned to the SelectCommand property of the another DataAdapter to make it ready to be used in the next step. J. The Fill() method of the DataAdapter is called to fi ll the StudentCourse table, which calls our second stored procedure to fi ll the StudentCourse table. K. If this calling is successful, the Count property should be greater than 0, which means that at least one row is fi lled into the StudentCourse table, and the user - defi ned method FillCourseList() is called to fi ll the CourseList box in the Student form with all courses (course_id) retrieved from the stored procedure. Otherwise, an error message is displayed if this fi ll has failed. L. If the user selects the DataReader method, the ExecuteReader() method is called to retrieve back all information related to the selected student and assign it to the sqlStudentReader object. M. If this method is executed successfully, which means that at least one row of data is read out and assigned to the DataReader, the HasRows property should be true, and the user - defi ned method FillStudentReader() is executed to fi ll the related information to six textboxes in the Student form. N. Otherwise, an error message is displayed to indicate this situation. O. The BuildCommand() method is called to build the StudentCourse Command object with our second stored procedure. The unique input parameter to this stored procedure is the StudentID that is added to the Parameters collection that is a property of the Command object. The fi nished Command object is assigned to the SelectCommand property of the StudentCourse DataAdapter. c05.indd 375c05.indd 375 2/11/2010 2:58:55 PM2/11/2010 2:58:55 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 376 Chapter 5 Data Selection Query with Visual C#.NET P. The ExecuteReader() method is called to run our second stored procedure to read out all courses taken by the selected student, and assign them to the StudentCourse DataReader object. Q. If the method ExecuteReader() runs successfully, the HasRows property of the DataReader object should be true, the user - defi ned method FillCourseReader() is executed to fi ll all courses (course_id) into the CourseList listbox. Otherwise if this method has failed, an error message is displayed. R. Two student DataReader objects are released before we can exit this method. S. The cleaning job is performed to release all other data objects used in this method. The code for the method FindName() is identical to what we developed for the same method in Section 5.18.5.2 . Refer to Figure 5.99 to get detailed information for this coding. In order to pick up the correct student ’ s image fi le from this subroutine, note that you must store all students ’ image fi les in the folder in which your Visual C# 2008 pro- ject ’ s executable fi le is located. In our application, this folder is C:\Book6\Chapter 5\ SQLSelectRTObject\bin\Debug. If you place those students ’ image fi les in another folder, you must provide a full name, which includes the drive name, path, and the image fi le name, for that student ’ s image fi le to be accessed, and assign it to the returning string variable strName in this method. The coding for the BuildCommand() method is shown in Figure 5.131 . This coding is straightforward and easy to be understood. First, a new LogInForm instance is created, and the getLogInForm() method is called to pick up the global con- nection object and assign it to the Connection property of the Command object. Then the Command object is initialized by assigning the related properties such as the CommandType and CommandText to it. The point is that the value assigned to the CommandType must be StoredProcedure, and the value assigned to the CommandText must be equal to the name of the stored procedure we developed in the last section. The codes for the methods FillStudentTextBox(), MapStudentTextBox(), and FillCourseList() are shown in Figure 5.132 . In total we need six pieces of information for the selected student, so a six - dimension textbox array StudentTextBox[5] is used (the index is based on 0). In the method FillStudentTextBox(), a double foreach loop is used to retrieve each queried column from the returned row. The outer loop is only executed one time since we have only retrieved back one data row from the Student table. Each fetched column is assigned to the associated textbox to be displayed in the Student form. The method private void BuildCommand(ref SqlCommand cmdObj, string cmdString) { LogInForm logForm = new LogInForm(); logForm = logForm.getLogInForm(); cmdObj.Connection = logForm.sqlConnection; cmdObj.CommandType = CommandType.StoredProcedure; cmdObj.CommandText = cmdString; } SQLSelectRTObject.StudentForm BuildCommand() Figure 5.131 Coding for the BuildCommand method. c05.indd 376c05.indd 376 2/11/2010 2:58:55 PM2/11/2010 2:58:55 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.19 Query Data Using Runtime Objects to SQL Server Database 377 MapStudentTextBox() is to set up a one - to - one correct relationship between each element in the textbox array and each associated textbox control in the Student form. The functionality of the method FillCourseList() is to pick up each matched row (course_id) from the fi lled StudentCourse table and add them into the CourseList listbox. The codes for the methods FillStudentReader() and FillCourseReader() are shown in Figure 5.133 . In the method FillStudentReader(), fi rst textbox object array is initialized, and then the MapStudentTextBox() method is executed to set up a correct relationship between each element in the textbox array and each associated textbox control in the Student form. A while and a for loop are utilized to pick up each fetched column and assign them to the associated textbox control. The point is the difference between the system method GetString() and GetValue() that belong to the DataReader class. The former can be used to get only string data stored in the database, but the latter can be used to get any kind of data stored in the database. The issue is that another system method, ToString(), must be attached to this GetValue() to convert the data from any other data type to the string and assign it to the Text property of each TextBox control. Therefore, the latter is more popular and more powerful in a data - driven application. You can try to compare these two methods yourself if you like. private void FillStudentTextBox(DataTable StudentTable) { int pos1 =0; for (int pos2 = 0; pos2 <= 6; pos2++) //Initialize the textbox array StudentTextBox[pos2] = new TextBox(); MapStudentTextBox(StudentTextBox); foreach (DataRow row in StudentTable.Rows) { foreach (DataColumn column in StudentTable.Columns) { StudentTextBox[pos1].Text = row[column].ToString(); pos1++; } } } private void MapStudentTextBox(Object[] sTextBox) { sTextBox[0] = txtID; //The order must be identical with the sTextBox[1] = txtGPA; //order in the query string - strStudent sTextBox[2] = txtCredits; sTextBox[3] = txtMajor; sTextBox[4] = txtSchoolYear; sTextBox[5] = txtEmail; } private void FillCourseList(DataTable StudentCourseTable) { CourseList.Items.Clear(); foreach (DataRow row in StudentCourseTable.Rows) { CourseList.Items.Add(row[0]); //the 1st column is course_id - strStudentCourse } } SQLSelectRTObject.StudentForm FillStudentTextBox() Figure 5.132 Coding for three user - defi ned methods. c05.indd 377c05.indd 377 2/11/2010 2:58:56 PM2/11/2010 2:58:56 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 378 Chapter 5 Data Selection Query with Visual C#.NET The functionality of the method FillCourseReader() is similar to the method FillStudentReader(). The only difference between them is that the Add() method is used to add each fetched row (course_id) to the listbox in the FillCourseReader() method. We almost completed all coding development for this project. Finally don ’ t forget to make coding for the Back command button. Enter this.Hide() into this method. Now we can begin to test calling those two stored procedures from our Visual C# project. Build the project and click on the Start Debugging button to run our project, enter user name and password, and select the Student Information item to open the Student form window, which is shown in Figure 5.134 . Select Ashly Jade from the Student Name combobox and click on the Select button. All information related to this student and the courses are displayed in the six textboxes and the CourseList box, which is shown in Figure 5.134 . Our calling to two stored procedures are very successful! Some readers may fi nd that these two stored procedures are relatively simple, and each procedure only contains one SQL statement. Let ’ s dig a little deeper and develop some sophisticated stored proce- dures and try to call them from our Visual C# project. Next we will develop a stored procedure that contains more SQL statements. 5.19.2.7.4 Query Data Using More Complicated Stored Procedures We want to get all courses (all course_ids) taken by the selected student based on student name from the StudentCourse table. To do that, we must fi rst go to the Student table to obtain the associated student_id based on the student name since there is no student name column available in the StudentCourse table. Then we can go to the StudentCourse table to pick up all course_id based on the selected student_id. We need to perform two queries to complete this data - retrieving operation. Now we try to combine these two queries into a private void FillStudentReader(SqlDataReader StudentReader) { int intIndex = 0; for (int pos2 = 0; pos2 <= 6; pos2++) //Initialize the textbox array StudentTextBox[pos2] = new TextBox(); MapStudentTextBox(StudentTextBox); while (StudentReader.Read()) { for (intIndex = 0; intIndex <= StudentReader.FieldCount - 1; intIndex++) StudentTextBox[intIndex].Text = StudentReader.GetValue(intIndex).ToString(); } } private void FillCourseReader(SqlDataReader StudentCourseReader) { int pos = 0; CourseList.Items.Clear(); while (StudentCourseReader.Read()) { for (pos = 0; pos <= StudentCourseReader.FieldCount - 1; pos++) CourseList.Items.Add(StudentCourseReader.GetValue(pos).ToString()); } } SQLSelectRTObject.StudentForm FillStudentReader() Figure 5.133 Coding for another two user - defi ned methods. c05.indd 378c05.indd 378 2/11/2010 2:58:56 PM2/11/2010 2:58:56 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.19 Query Data Using Runtime Objects to SQL Server Database 379 single stored procedure to simplify our data - querying operation. First let ’ s create our stored procedure. Open Visual Studio.NET and open the Server Explorer window; click on the plus symbol icon that is next to the 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. 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 shown in Figure 5.135 into this new procedure. Figure 5.134 Running status of the Student form. Figure 5.135 New stored procedure: StudentCourseINTO. c05.indd 379c05.indd 379 2/11/2010 2:58:56 PM2/11/2010 2:58:56 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 380 Chapter 5 Data Selection Query with Visual C#.NET Let ’ s give a detailed discussion for this piece of coding. A. The stored procedure is named dbo.StudentCourseINTO. B. The input parameter is the student name, @stdName, which is a varying - char variable with the maximum characters of 50. All parameters, no matter input or output, must be declared inside the braces. C. The local variable @stdID is used to hold the returned query result from the fi rst SQL statement that retrieves the student_id. D. T h e fi rst SQL statement is executed to get the student_id from the Student table based on the input parameter @stdName. A SET command must be used to assign the returned result from the fi rst SQL query to the local variable (or intermediate variable) @stdID. The fi rst SQL statement must be covered by the parenthesis to indicate that this whole query will be returned as a single data. E. The second SQL statement is executed, and this query is used to retrieve all courses (course_id) taken by the selected student from the StudentCourse table based on the student_id (@stdID) obtained from the fi rst query. F. Finally the queried result, all courses or course_id, is returned. Go to File|Save StoredProcedure1 to save this stored procedure. Now let ’ s test our stored procedure in the Server Explorer window. Right - click on our new created stored procedure StudentCourseINTO and select the Execute item from the pop - up menu. On the opened dialog box, enter the student ’ s name, Erica Johnson; then click on the OK button to run the procedure. The running result is shown in Figure 5.136 . Figure 5.136 Running result of the stored procedure. c05.indd 380c05.indd 380 2/11/2010 2:58:58 PM2/11/2010 2:58:58 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5.19 Query Data Using Runtime Objects to SQL Server Database 381 Next we need to develop a Visual C# project to call this stored procedure to test the functionality of the stored procedure. To save time and space, we added a new form window into this project and named it SPForm. Open our project SQLSelectRTObject and select Project | Add Windows Form item, enter SP Form.cs into the Name: box and click on the Add button to add this new form into our project. Enter “ SPForm ” into the name property as the name for this form. Enlarge the size of this SP Form by dragging the border of the form window, and then open the Student form window. We need to copy all controls on the Student form to our new SP form. On the opened Student form, select Edit|Select All and Edit|Copy items, and then open the SP form and select Edit|Paste to paste all controls we copied from the Student form. To save time, next we need to copy most codes from the Student code window to our new SP form code window. But fi rst you need to add the SQL Server - related namespace System.Data.SqlClient to the namespace declaration section on this SP form code window. The only difference is the codes for the Select button Click method, cmdSelect_Click(). Don ’ t copy this piece of code since we need to develop new codes to test our stored procedure later. To copy all other codes, open the code window of the Student form, select those codes, copy, and then paste them to our new SP form code window. One point you need to note is that when you copy the codes for two methods, such as the constructor of the SP Form and the Back button Click, you must fi rst open the constructor or the Back method in the SP form, and only copy the body of those methods without including the header and ender of those methods. Now let ’ s develop the codes for our Select button Click method. Most codes are identical to those we developed for the Student form, such as the methods FindName() and FillCourseReader(). Open the Select button Click method by double - clicking on the Select button from the Designer window, and enter the code shown in Figure 5.137 into this method. Let ’ s discuss this piece of code step by step to see how it works. A. The name of our stored procedure, “ dbo.StudentCourseINTO ” , must be declared fi rst, and this name must be identical with the name we used when we created the stored pro- cedure in the Server Explorer window. B. All data components, including the Command, DataAdapter, DataTable, and DataReader objects, are declared here since we need to use them for this data query operation. C. The method FindName() is called to get and display the matched student image fi le, and the returned name of the image fi le is stored in the local string variable strName. D. The Command object is initialized with suitable properties by executing the user - defi ned method BuildCommand(). The CommandType property must be StoredProcedure to indicate that this query is to execute a stored procedure. The CommandText property must be equal to the name of our stored procedure, “ dbo.StudentCourseINTO ” , which is stored in a string variable strStudentCourse. E. The input parameter to the stored procedure is the student name, which is obtained from the student combobox, and it should be added into the Parameters collection property of the Command object. You need to note that the nominal name @stdName must be identi- cal with the input parameter name we defi ned in the parameter braces in our stored procedure dbo.StudentCourseINTO. The real parameter is entered by the user as the project runs. The fi nished Command object is assigned to the SelectCommand property of the DataAdapter, which will be used later to fetch the desired course_id from the StudentCourse table. c05.indd 381c05.indd 381 2/11/2010 2:58:59 PM2/11/2010 2:58:59 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 382 Chapter 5 Data Selection Query with Visual C#.NET F. If the user selected the DataAdapter Method, the Fill() method is called to fi ll the StudentCourse table with the desired course_id. G. If the Count property is greater than 0, this fi ll is successful. The user - defi ned method FillCourseList() is called to fi ll the fetched course_id into the CourseList listbox. Otherwise an error message is displayed to indicate this situation. H. If the user selected the DataReader Method, the ExecuteReader() method is executed to invoke the DataReader to call our stored procedure. I. If this call is successful, the queried result should be stored in the DataReader with certain rows. The user - defi ned method FillCourseReader() is executed to fi ll the returned course_id into the CourseList box in that situation. Otherwise an error message is displayed if this call is failed. J. The cleaning job is performed to release all objects used in this data query operation. Most user - defi ned methods, such as BuildCommand() and FindName(), are identical with those methods in the Student form without any modifi cation. For your convenience, we show the other two user - defi ned methods FillCourseList() and FillCourseReader() again in Figure 5.138 . private void cmdSelect_Click(object sender, EventArgs e) { string strStudentCourse = "dbo.StudentCourseINTO"; SqlDataAdapter StudentCourseDataAdapter = new SqlDataAdapter(); SqlCommand sqlCmdStudentCourse = new SqlCommand(); DataTable sqlStudentCourseTable = new DataTable(); SqlDataReader sqlStudentCourseReader; string strName = string.Empty; strName = FindName(ComboName.Text); if (strName == "No Match") MessageBox.Show("No Matched Student's Image Found!"); BuildCommand(ref sqlCmdStudentCourse, strStudentCourse); sqlCmdStudentCourse.Parameters.Add("@stdName", SqlDbType.Char).Value = ComboName.Text; StudentCourseDataAdapter.SelectCommand = sqlCmdStudentCourse; if (ComboMethod.Text == "DataAdapter Method") { StudentCourseDataAdapter.Fill(sqlStudentCourseTable); if (sqlStudentCourseTable.Rows.Count > 0) FillCourseList(sqlStudentCourseTable); else MessageBox.Show("No matched course_id found!"); } else //DataReader Method is selected { sqlStudentCourseReader = sqlCmdStudentCourse.ExecuteReader(); if (sqlStudentCourseReader.HasRows == true) FillCourseReader(sqlStudentCourseReader); else MessageBox.Show("No matched course_id found!" ); sqlStudentCourseReader.Close(); } sqlStudentCourseTable.Dispose(); StudentCourseDataAdapter.Dispose(); sqlCmdStudentCourse.Dispose(); } A B C D E F G H I J SQLSelectRTObject.SPForm cmdSelect_Click() Figure 5.137 Coding for the Select button Click method. c05.indd 382c05.indd 382 2/11/2010 2:58:59 PM2/11/2010 2:58:59 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... 10g Express Edition Release 2 In this section, we use the Oracle Database 10g Express Edition Release 2 (Oracle Database 10g XE R2) as our database provider Oracle Database 10g Express Edition (Oracle Database XE R2) is an entry-level, small-footprint starter database with the following advantages: 406 Chapter 5 Data Selection Query with Visual C#.NET • Free to download and install on your local computer... been created and located at the default database folder Now let’s go to the last part in this chapter—develop a data-driven application using the real-time object with the Oracle database 5.20 QUERY DATA USING RUNTIME OBJECTS TO ORACLE DATABASE For your convenience, in this section we will use our sample database CSE_DEPT developed in Chapter 2 with the Oracle Database 10g Express Edition Release 2... only need to download and install the Oracle Database XE R2 Server component since it provides both an Oracle database and tools for managing this database It also includes the Client component of Oracle Database XE, so that you can connect to the database from the same computer on which you installed the Server, and then administer the database and develop Visual C# 2008 applications Before we can start... applications with the following specific functionalities: • The Oracle Database 10g XE R2 can be easily upgraded to Standard Edition One, Standard Edition, and Enterprise Edition • Any application developed for Oracle Database XE will run completely unchanged with Oracle Database 10g Standard Edition One, Standard Edition, or Enterprise Edition, and the application development investment is guaranteed • With. .. development investment is guaranteed • With Oracle Database XE R2, ISVs have the industry’s leading database technology to power their applications Distributing Oracle Database XE R2 in their applications or products without additional cost offers even greater value to their customers • Oracle Database XE R2 can be freely distributed as a stand-alone database or as part of a third-party application or... copy the completed database file CSE_DEPT.mdf into another folder in your computer; in our case, it is C: \database folder, and connect your project to this database file using the Server Explorer You can use the same database file CSE_ DEPT.mdf that is located at the default SQL Server 2005 database folder, C:\Program Files\Microsoft SQL Server 2005\MSSQL.1\MSSQL\Data, as your target database and connect... connection string With the connection string ready, now we can start to develop our sample projects First let’s start with the general runtime objects method 5.20.3 Query Data Using General Runtime Objects First, we need to create a new Visual C# 2008 Windows-Based project named OracleSelectRTObject For your convenience, a blank project with five forms has been 408 Chapter 5 Data Selection Query with Visual C#.NET... of the connection string for the Oracle Database 10g XE R2 5.20.2 Configure Oracle Database Connection String As we mentioned in Section 5.19.1, there are different ways to build a connection string for the Oracle database connection One way is to use the database alias defined in the tnsnames.ora file This file is created automatically after you install the Oracle database 10g XE R2 on your machine During... Word The 5.20 Query Data Using Runtime Objects to Oracle Database Figure 5.160 407 Sample of the file tnsnames.ora database alias for our application is XE, and the top block of this file is the definition of the database alias (refer to Figure 5.160) Close this file and now let’s create our connection string for the Oracle database 10g XE R2 using the database alias XE The connection string can be defined... faculty’s image An error message will be displayed if this search has failed 396 Chapter 5 Data Selection Query with Visual C#.NET C If the user selected the first method, LINQ to SQL method, a standard LINQ query structure is adopted with an implicit typed local variable f_info with a data type var The Visual C# 2008 can automatically convert this var to any suitable data type; in this case, it is a collection . Selection Query with Visual C#. NET F. If the user selected the DataAdapter Method, the Fill() method is called to fi ll the StudentCourse table with the desired. Query with Visual C#. NET Open the SelectionForm and enter the codes shown in Figure 5.147 into the code window of this form. Most of code is identical with

Ngày đăng: 20/10/2013, 11:15

Từ khóa liên quan

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

Tài liệu liên quan