Frog Recommendation: A simple example of passing DataSet with a web service
First, statement: Even the newcomers written by ASP.NET, the rough places Welcome to the master guidance, this example is written, there is no more optimization, such as adding the web service to the local Application Cache, there are joins Safety authentication, etc., the emergence of web services is undoubtedly a good thing, we have to master this technology, start with the simplest example. Generally, the process used by web services is like this : Web service pays the user to find the disco file of the web service by querying the UDDI directory, then query the Disco file to find the URL described in the web service, use the WSDL file to create and understand the SOAP packet sent to and the web service. So data can be passed once, by the way, SOAP can pass a lot of types, if the data type with POST or GET is transmitted, it is more complicated, but next time I intend to write a use ASP, VBS, XMLHTTP to apply an example of a Web service.
Before using the example, confirm that you have installed the .NET framework and SQLServer2000 and its default sample database, the database connection code is modified to fit the code of the database environment, such as the account and password.
Open VS.NET, create a new ASP.NET (C #) solution WAWA, add a web service: WAWASERVICE.ASMX imports a namespace on the original basis, use the following statement: use system.data.sqlclient; then plus a public method wawa_getDataSet, this method is to call a method and returns a getDataSet DataSet, and finally to ensure that the following code using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; Using system.web.services; using system.data.sqlclient;
Namespace WAWA {
[WebService (Name = "wawa Service", Description = "wawa", Namespace = "wawa")] public class wawaService: System.Web.Services.WebService {[WebMethod (Description = "wawa")] public DataSet wawa_getDataSet (string str) {return getDataSet (str_wawa_sql);} private DataSet getDataSet (string sql) {string connString = "server = (local); database = Northwind; uid = sa; pwd = sa;"; SqlConnection conn = new SqlConnection (connString) Conn.open (); SqlDataAdapter Da = New SqlDataAdapter (SQL, Conn); DataSet DS = New Dataset (); DA.FILL (DS, "EMPLOYEES); RETURN DS;} PUBLIC WAWASERVICE () {// Constructor InitializationComponent ();
#REGION component designer generated code // Web service designer essential private icontainer components = null;
PRIVATE VOID Initialization () {}
Protected Override Void Dispose (Bool Disposing) {if (Disposing && Components! = null) {Components.dispose ();} Base.dispose (Disposing);} #ENDREGON}} Open http://192.168.0.110/wawa /wawaservice.asmx test (note the IP address to your own IP, no Localhost) Now we generate the agent locally, you need to use the wsdl.exe command, this command should be installed .NET framework The following information is created in the system's command mode: (note that if you can't run the WSDL directly in the cmd command mode, find this file and configure it to configure the system variable of the server's environment variable. In the PATH variable, it is written by using a half-angle English symbol separated between the various paths. This place may say a little dizzy. If you don't listen, you can run the "Visual Studio .NET 2003 Command Tips" tool. The following CSC command is the same, some people say: "Visual Studio .NET 2003 Command Tips", I poured .WSDL / Language: CS / Namespace: wawa /out:wawaproxy.cs http: //192.168 .0.110 / wawa / wawasempice.asmx? WSDL
Display information is similar to:
E: /ME/web.net/wawa> WSDL / Language: CS / Namespace: WAWA /OUT :WAWAPROXY.CS http://192.168.0.110/wawa/wawaservice.asmx?wsdlmicrosoft (r) Web service Description Language Utility [Microsoft (R) .NET Framework, Version 1.1.4322.573] Copyright (c) Microsoft Corporation 1998-2002. All Rights Reserved.
Write "WAWAPROXY.CS".
E: /ME/web.net/wawa>
Use Notepad to open the generated WAWAPROXY.CS file, observe, almost as follows ~ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------- //
// // This source code is automatically generated by WSDL, Version = 1.1.4322.573.
// namespace wawa {using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.ComponentModel; using System.Web.Services; ///
Return ((System.Data.DataSet);}}} We have to compile the WAWAPROXY.CS file into a dynamic link library, with the following command CSC / T: library /R :system.Web. Services.dll /out:wawaproxy.dll WAWAPROXY.CS The prompt appears to be below:
E: /ME/web.net/wawa> CSC / T: library /r :system.web.services.dll /out:wawaproxy.dllwawaproxy.csmicrosoft (r) Visual C # .NET compiler version 7.10.3052.4 For Microsoft (R) .NET Framework version 1.1.4322 Copyright (C) Microsoft Corporation 2001-2002. all rights reserved.
Then copy the generated wawaproxy.dll into the bin directory in the current directory. Add a WAWASERVICE.ASPX form, which is used to call the web service and display the data, and import two namespaces in the background encoding. Using system.web.services; Using System.data.sqlclient; Drag a DataGrid coming in, name the DataGridWawa assurance page Similar to: <% @ page language = "c #" codebehind = "wawaservice.aspx.cs" autoeventwireup = "false" inherits = "wawa.wawaservice1"%>