Web Service: Learning Web Service series with demo (6) -- Accessing Database Web Services in disconnected Mode

Source: Internet
Author: User
Tags connectionstrings
Through the previous study on Web Services: Using demo to learn Web Services Series (5) -- Accessing Database Web Services in connection mode, we already know that, how does web services access the database in connection mode from the database. Next, let's take a look at the change in WebService described in the previous discussion, so that this WebService can become a web service that accesses the database in disconnected mode.
This time we want to change the previous WebService, instead of making changes in the original method, but adding a new method to WebService and Program A new windows form is added to the project to call the new method.
1. Add the following method to the previous WebService, Code As follows: 1 [Webmethod]
2 Public Dataset selectuser ( String Username)
3 {
4 Configuration webconfig = Webconfigurationmanager. openwebconfiguration ( " ~ /Web. config " );
5 Dataset DS =   New Dataset ( " Wsdemodb " );
6 Datatable dt =   New Datatable ( " Usertable " );
7 DS. namespace =   " Http://tempuri.org/DataSet " ;
8 DS. Tables. Add (DT );
9 If (Webconfig. connectionstrings. connectionstrings. Count >   0 )
10 {
11 Connectionstringsettings constr = Webconfig. connectionstrings. connectionstrings [ " Wsconstringsql " ];
12 If (Constr ! =   Null )
13 {
14 Sqlconnection sqlcon =   New Sqlconnection (constr. connectionstring );
15 Sqlcommand sqlcom =   New Sqlcommand ( " Select User ID, user name, user password, user name, user gender, password prompt, answer: From User table where (user name like '%' + @ user name + '% ') " , Sqlcon );
16 Sqlcom. Parameters. Add ( " @ User Name " , Sqldbtype. nvarchar );
17 Sqlcom. Parameters [ " @ User Name " ]. Value = Username;
18 Sqldataadapter sqlda =   New Sqldataadapter (sqlcom );
19 Try
20 {
21Sqlcon. open ();
22Sqlda. Fill (DT );
23}
24 Finally
25 {
26 Sqlda. Dispose ();
27 Sqlcom. Dispose ();
28 Sqlcon. Close ();
29 Sqlcon. Dispose ();
30 }
31 }
32 }
33 Return DS;
34 }

WebService method Description: The added method is named selecuser, and a parameter username must be obtained. This method returns a dataset of user information queried based on the input username.
2. Add the form in the C/S project and add the relevant code in the "query" button. The form and Code are as follows:
1. Add a Textbox, a button, and a datagridview to the form, such:

2. The code under the "query" button is: 1 Private   Void Btn_selectuser_click ( Object Sender, eventargs E)
2 {
3 Myserv. myserviceclass mywebserv =   New Myserv. myserviceclass ();
4 Dataset DS =   New Dataset ();
5 DS = Mywebserv. selectuser (tb_user.text );
6 If (Ds. Tables. Count >   0 )
7 {
8Dgv_userview.datasource=DS. Tables ["Usertable"];//It can also be written as: dgv_userview.datasource = Ds. Tables [0];
9}
10 Else
11 {
12MessageBox. Show ("No data is found!");
13}
14 }

This completes the WebService call.
Code Description: Like other calls, we need to instantiate the WebService proxy class, then define a dataset to receive the returned value of selectuser in WebService, and finally bind the returned dataset to dgv_userview.

conclusion: this time, we can see that the returned WebService value is dataset, but in Visual Studio. the Web Services created in net2005 can return the datatable, which is in Visual Studio. net2003 does not work, and the system will prompt "unable to serialize ". So far, we have finished describing the simple method for the c/s program to call Web Services. Next time, we will explain some Web Services principles, soap messages, and XML.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.