Tài liệu Practical Database Programming With Visual C#.NET- P17 docx

50 590 0
Tài liệu Practical Database Programming With Visual C#.NET- P17 docx

Đ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

9.5 Build ASP.NET Web Service to Update and Delete Data for SQL Server Database 823 Figure 9.86 Running result for the Web method GetSQLCourse. Figure 9.87 Parameter - input Web interface. c09.indd 823c09.indd 823 2/11/2010 3:02:07 PM2/11/2010 3:02:07 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 824 Chapter 9 ASP.NET Web Services Figure 9.89 Parameter - input interface. Figure 9.88 Running result of the Web method GetSQLCourseDetail. To confi rm this data deleting, close the current running result interface shown in Figure 9.90 and click on the Back button to return to the home page of the Web Service project. Click on the Web method GetSQLCourse to run it to pick up all courses taught by the selected faculty Ying Bai . Enter the faculty name Ying Bai into the Value box as the input parameter to this method and click on the Invoke button to run it. The running result is shown in Figure 9.91 . From the running result shown in Figure 9.91 , it can be found that the course with the course_id of CSE - 526 has been deleted from the Course table. c09.indd 824c09.indd 824 2/11/2010 3:02:08 PM2/11/2010 3:02:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 9.5 Build ASP.NET Web Service to Update and Delete Data for SQL Server Database 825 Figure 9.90 Running result of the Web method SQLDeleteSP. Figure 9.91 Running result of the Web method GetSQLCourse. c09.indd 825c09.indd 825 2/11/2010 3:02:09 PM2/11/2010 3:02:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 826 Chapter 9 ASP.NET Web Services To get a clearer picture for this data deleting, let ’ s try to run another Web method GetSQLCourseDetail(). Close the current running result interface shown in Figure 9.91 and click on the Back button to return to the home page. Select and click the Web method GetSQLCourseDetail to try to run it. Enter CSE - 526 as the course_id to the Value box as the input parameter to this method and click on the Invoke button to run it. The running process becomes very slow. The reason for this is because a message box is displayed behind the top page. Try to move the current top page to either side of the screen, and you can fi nd that a message box with a message "No matched course found" has shown up. This means that the queried course has been deleted from the Course table and it cannot be found from that table again. Click on the OK button to the message box to close it, and the running result is dis- played, which is shown in Figure 9.92 . The following returned values are displayed for two member data: • SQLOK: false • SQLError: No matched course found This is identical with the warning message displayed in the message box as this method runs. Close the current page and our Web Service project. Our Web Service project is very successful. As a reminder, it is highly recommended to recover all deleted data from all tables in our sample database. To do that, open our sample database and the Course table from either the Server Explorer in Visual Studio.NET or Microsoft SQL Server Management Figure 9.92 Running result of the Web method GetSQLCourseDetail. c09.indd 826c09.indd 826 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 9.7 Build Web-Based Web Service Clients to Use Web Services 827 Studio, and add all pieces of course information shown in Table 9.6 in Section 9.5.3.2 in this chapter for the deleted course CSE - 526 into our Course table. You can remove all message box methods MessageBox() from this Web Service project to speed up the execution of this Web Service if you like. A completed Web Service project WebServiceSQLUpdateDelete can be found at the folder DBProjects\ Chapter 9 located at the accompanying ftp site (see Chapter 1 ). Next let ’ s take care of building some Windows - based and Web - based client projects to use this Web Service. 9.6 BUILD WINDOWS - BASED WEB SERVICE CLIENTS TO USE WEB SERVICES In order to save space, Section 9.6, which provided a detailed discussion in how to build a Windows - based client project WinClientSQLUpdateDelete to consume our Web Service project WebServiceSQLUpdateDelete, has been moved to the accompanying ftp site with a fi le named WinClientSQLUpdateDelete.pdf that can be found from the folder DBProjects\Chapter 9\Doc that is located at the site ftp://ftp.wiley.com/public/ sci_tech_med/practical_database . For your convenience, a completed Windows - based client project, WinClientSQLUpdateDelete, has also been developed and debugged, which can be found from the folder DBProjects\Chapter 9 at the same ftp site. 9.7 BUILD WEB - BASED WEB SERVICE CLIENTS TO USE WEB SERVICES There is no signifi cant difference between building a Windows - based and a Web - based client project to use a Web Service. To save time and space, we try to modify an existing Web - based client project WebClientSQLInsert we developed in the previous section to make it our new Web - based client project WebClientSQLUpdateDelete. Actually we can copy and rename that entire project as our new Web - based client project. However, we prefer to create a new ASP.NET website project, and then copy and modify the Course page. This section can be developed in the following sequences: 1. Create a new ASP.NET website project WebClientSQLUpdateDelete and add an existing website page Course.aspx from the project WebClientSQLInsert into our new project. 2. Add a Web Service reference to our new project and modify the Web form window of the Course.aspx to meet our data updating and deleting requirements. 3. Modify the codes in the related methods of the Course.aspx.cs fi le to call the associated Web method to perform our data updating and deleting. The code modifi cations include the following sections: a. Remove the Insert button ’ s Click method cmdInsert_Click() since we do not need any data insertion action in this application. b. Remove the user - defi ned FillCourseDataSet() method since no DataSet method will be used in this application. c09.indd 827c09.indd 827 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 828 Chapter 9 ASP.NET Web Services c. Remove the TextChanged event method of the Course ID textbox since we do not need this event and its event method in this application. d. Modify the codes inside the Page_Load() method. e. Develop the codes for the Update button ’ s Click method. f. Develop the codes for the Delete button ’ s Click method. g. Modify the codes in the Select button ’ s Click method and the related methods such as ProcessObject() and FillCourseListBox(). h. Modify the codes in the SelectedIndexChanged event method of the course listbox control and the related method FillCourseDetail(). Now let ’ s start these modifi cations with the fi rst step listed above. 9.7.1 Create New Website Project and Add Existing Web Page Open Visual Studio.NET and go to the File|New Web Site menu item to create a new website project. Enter C:\Chapter 9\WebClientSQLUpdateDelete into the name box that is next to the Location box, and click on OK to create this new project. On the opened new project window, right - click on our new project icon WebClientSQLUpdateDelete from the Solution Explorer window, and select the item Add Existing Item from the pop - up menu to open the Add Existing Item dialog box. Browse to our Web project WebClientSQLInsert, select it, and then click on the Add button to open all existing items for this website project. Select both items, Course.aspx and Course.aspx.cs , from the list and click on the Add button to add these two items into our new website project. 9.7.2 Add Web Service Reference and Modify Web Form Window To add a Web reference of our Web Service to this new Website project, right - click on our new project icon from the Solution Explorer window and select the item Add Web Reference from the pop - up menu. Now open our Web Service project WebServiceSQLUpdateDelete and click the Start Debugging button to run it. As the project runs, copy the URL from the Address box and paste it into the URL box in our Add Web Reference dialog box. Then click on the green Go button to search and add this Web Service as a reference to our client project. You can modify this Web reference name to any name you want. In this application, we prefer to change it to WS_ SQLUpdateDelete . Your fi nished Add Web Reference dialog box should match the one shown in Figure 9.93 . Click on the Add Reference button to fi nish this adding Web reference process. Immediately you can fi nd that the following three fi les are created in the Solution Explorer window under the folder App_WebReferences : • WebServiceSQLUpdateDelete.disco • WebServiceSQLUpdateDelete.discomap • WebServiceSQLUpdateDelete.wsdl c09.indd 828c09.indd 828 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 9.7 Build Web-Based Web Service Clients to Use Web Services 829 The only modifi cation to the Web page of the Course.aspx is the DropDownList box control ComboMethod . Because we only use one method to perform the data updat- ing and deleting action in our Web Service project, we do not need the Method combobox control in this application. We can remove this control from the graphic user interface. However, it does not matter if we keep it without using it in our Web client project. Our fi nished graphic user interface is shown in Figure 9.94 . Now let ’ s take care of modifi cations to the codes in related user - defi ned methods in the Course.aspx page. 9.7.3 Modify Codes for Related Methods Before we can perform any code modifi cation, fi rst we need to perform the following operations to delete some unused methods in our new project: 1. Remove the Insert button ’ s Click method cmdInsert_Click() since we do not need any data insertion action in this application. 2. Remove the user - defi ned FillCourseDataSet() method since no DataSet method will be used in this application. 3. Remove the TextChanged event method of the Course ID textbox since we do not need this event and its event method in this application. The fi rst code modifi cation is to change the codes in the Page_Load() method and some global variables. Figure 9.93 Finished Add Web Reference dialog box. c09.indd 829c09.indd 829 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 830 Chapter 9 ASP.NET Web Services 9.7.3.1 Modify Codes in Page_Load Method Perform the following changes to complete this modifi cation: 1. Remove the fi eld - level variables dsFlag and wsDataSet . 2. Change the name of the base class for the fi eld - level instance wsSQLResult from WS_ SQLInsert.SQLInsertBase to WS_SQLUpdateDelete.SQLBase . 3. In the Page_Load() method, remove the code that is used to add and display the second Web method, DataSet Method , from the ComboMethod control. Your modifi ed codes for the Page_Load() method should match that shown in Figure 9.95 . The modifi ed codes have been highlighted in bold. The next step is to develop the codes for the Update button ’ s Click method. 9.7.3.2 Develop Codes for Update Button ’s Click Method The function of this method is: When a faculty name is selected and all six pieces of updating course information are entered in the six textbox controls, the updating course information will be passed to the Web method SQLUpdateSP() we developed in our Web Service project and a stored procedure WebUpdateCourseSP() is executed to perform this course updating action as the Update button is clicked on by the user. Now let ’ s double - click on the Update button to open its Click method, and enter the codes shown in Figure 9.96 into this method. Let ’ s take a closer look at this piece of code to see how it works. A. A new instance of our Web proxy class, wsSQLUpdate, is created, and this instance is used to access the Web method SQLUpdateSP() we developed in our Web Service class Figure 9.94 Modifi ed graphic user interface. c09.indd 830c09.indd 830 2/11/2010 3:02:12 PM2/11/2010 3:02:12 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 9.7 Build Web-Based Web Service Clients to Use Web Services 831 public partial class Course : System.Web.UI.Page { WS_SQLUpdateDelete.SQLBase wsSQLResult = new WS_SQLUpdateDelete.SQLBase(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) //these items can only be added into the combo box in one time { ComboName.Items.Add("Ying Bai"); ComboName.Items.Add("Satish Bhalla"); ComboName.Items.Add("Black Anderson"); ComboName.Items.Add("Steve Johnson"); ComboName.Items.Add("Jenney King"); ComboName.Items.Add("Alice Brown"); ComboName.Items.Add("Debby Angles"); ComboName.Items.Add("Jeff Henry"); ComboMethod.Items.Add("Stored Procedure Method"); } } Course Page_Load() Figure 9.95 Modifi ed Page_Load method. protected void cmdUpdate_Click(object sender, EventArgs e) { WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLUpdate = new WS_SQLUpdateDelete.WebServiceSQLUpdateDelete(); string errMsg; try { wsSQLResult = wsSQLUpdate.SQLUpdateSP(ComboName.Text, txtCourseID.Text, txtCourseName.Text, txtSchedule.Text, txtClassRoom.Text, Convert.ToInt32(txtCredits.Text), Convert.ToInt32(txtEnroll.Text)); } catch (Exception err) { errMsg = "Web service is wrong: " + err.Message; Response.Write("<script>alert('" + errMsg + "')</script>"); } if (wsSQLResult.SQLOK == false) Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>"); } A B C D E Course cmdUpdate_Click() Figure 9.96 Codes for the Update button Click method. WebServiceSQLUpdateDelete to perform the data updating action against our sample database. B. A local string variable errMsg is also created, and it is used to reserve the error source that will be displayed as a part of an error message later. C. A try … catch block is used to call the Web method SQLUpdateSP() with six pieces of course updating data to execute a stored procedure WebUpdateCourseSP() to perform this course updating action. D. An error message will be displayed if any error is encountered during that data updating action. Note the displaying format of this error message. To display a string variable in a message box in the client side, one must use the Java script function c09.indd 831c09.indd 831 2/11/2010 3:02:13 PM2/11/2010 3:02:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 832 Chapter 9 ASP.NET Web Services alert() with the input string variable as an argument that is enclosed and represented by ' " + input_string + " ' . E. Besides the system error checking, we also need to check the member data SQLOK that is defi ned in our base class in the Web Service project to make sure that this data updating is application error free. A returned false value of this member data indicates that this data updating encountered some application error, and the error source stored in another member data SQLError is displayed using the Java script function alert() . In a similar way, we can develop the codes for the Delete button ’ s Click method to perform the data deleting actions against our sample database. 9.7.3.3 Develop Codes for Delete Button ’ s Click Method The function of this method is: When a course_id has been selected either from the listbox control or from the Course ID textbox control in this client page window, the selected course with a primary key that equals to that course_id will be deleted from all tables, including the child and parent tables, from our sample relational database. Double - click on the Delete button from our client page window to open the Delete Click method, and enter the codes shown in Figure 9.97 into this method. Let ’ s take a closer look at this piece of code to see how it works. A. A new instance of our Web proxy class, wsSQLDelete , is created, and this instance is used to access the Web method SQLDeleteSP() we developed in our Web Service class WebServiceSQLUpdateDelete to perform the data deleting action in our sample database. B. A local string variable errMsg is also created, and it is used to reserve the error source that will be displayed as a part of an error message later. C. A try … catch block is used to call the Web method SQLDeleteSP() with one piece of course information, course_id , that works as an identifi er to run a stored procedure WebDeleteCourseSP() to perform this course deleting action in our sample database. protected void cmdDelete_Click(object sender, EventArgs e) { WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLDelete = new WS_SQLUpdateDelete.WebServiceSQLUpdateDelete(); string errMsg; try { wsSQLResult = wsSQLDelete.SQLDeleteSP(txtCourseID.Text); } catch (Exception err) { errMsg = "Web service is wrong: " + err.Message; Response.Write("<script>alert('" + errMsg + "')</script>"); } if (wsSQLResult.SQLOK == false) Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>"); } A B C D Course cmdDelete_Click() E Figure 9.97 Codes for the Delete button Click method. c09.indd 832c09.indd 832 2/11/2010 3:02:13 PM2/11/2010 3:02:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... that exists in the connection strings between these two kinds of databases Second, as we know, ADO.NET provides different Data Providers to support users to access the different databases, and these Data Providers are database dependent, which means that different Data Providers are needed to access the different databases For the Oracle database, ADO.NET provides the namespace System.Data.OracleClient... Oracle database environment As you know, to develop a stored procedure that 844 Chapter 9 ASP.NET Web Services returns data in the Oracle database, one must build a Package in the Oracle database since a stored procedure developed in Oracle database won’t return any data Refer to Section 5.20.3.7 in Chapter 5 to get more detailed information on building and developing a Package in the Oracle database. .. Oracle database Many different methods can be used to build a Package in Oracle database In this section we want to use the Object Browser page in Oracle Database 10g Express Edition (XE) to build this Package Open the Oracle Database 10g XE home page by going to Start|All Programs |Oracle Database 10g Express Edition|Go To Database Home Page items Enter the correct username CSE_DEPT and password reback... data stored in an Oracle database In other words, to use matched data components provided by the ADO NET to access an Oracle database, one must use the associated namespace to access those data components Third, the prototype and structure of a stored procedure are different for the different databases To call a stored procedure to perform a data action against an SQL Server database is totally different... action against an Oracle database For differences 4 and 5 listed above, it is clear that the format of a query string is different when calling the different databases Also the nominal name of the dynamic parameter is distinguished when it is used for the different databases Based on the above discussion and analysis as well as the similarity between the SQL Server and Oracle databases, we try to develop... we will discuss how to access and manipulate data in the Oracle database via ASP.NET Web Services 9.8 BUILD ASP.NET WEB SERVICE PROJECT TO ACCESS ORACLE DATABASE Basically, the procedure to build an ASP.NET Web Service to access the Oracle database is very similar to the procedure to build an ASP.NET Web Service to access the SQL Server database The main differences are: 1 The connection string defined... CSE-665 for our Course table since we want to keep our database neat and complete You can recover this data by using one of the following five methods: 1 Use the Server Explorer window in Visual Studio.NET to open our sample database CSE_DEPT.mdf and our Course data table 2 Use the Microsoft SQL Server Management Studio or Studio Express to open our sample database CSE_DEPT.mdf and our Course data table 3... output parameter with a data type of Cursor, some special processing steps on this parameter are needed D Modify the query string by replacing the content of this string with the name of the Package we developed in the Oracle database in the last section The point is that both the Package’s name (WebSelectFaculty) and the stored procedure’s name (SelectFaculty) must be used together with the dot operator... a Web Service to access an SQL Server database or to access an Oracle database For example, you can use any client projects, such as either WinClientSQLSelect or WebClientSQLSelect, which we developed in the previous sections, to consume this Web Service project WebServiceOracleSelect with small modifications The main modification is to replace the Web reference with a new Web reference class, which... Service project to perform the data insertion to the Oracle database 9.10 BUILD ASP.NET WEB SERVICE PROJECT TO INSERT DATA INTO ORACLE DATABASE Basically, the procedure to build an ASP.NET Web Service to insert data into the SQL Server database is very similar to the procedure to build an ASP.NET Web Service to insert data into the Oracle database The main differences are listed below: 1 The connection . all tables in our sample database. To do that, open our sample database and the Course table from either the Server Explorer in Visual Studio.NET or Microsoft. Providers are database dependent, which means that different Data Providers are needed to access the different databases. For the Oracle database, ADO.NET

Ngày đăng: 14/12/2013, 15: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