Frog recommendation: a simple example of using web services to transmit Dataset
First of all, I declare that I am a beginner in Asp.net. If I write something rough, you are welcome to instruct the experts to correct it. In this example, I wrote it for time without doing too much optimization, for example, adding the Web service to the local application cache, and adding secure identity authentication. The emergence of Web Services is undoubtedly a good thing for us, we have to master this technology slowly, starting with the simplest example.
In general, the process of using Web Services is as follows: Web service paying users query the UDDI directory to find the disco file of Web Services, and then query the disco file to find the URL of the WSDL description in Web Services, you can use the WSDL file to create and understand the soap data packets sent to and sent by the web service. in this way, the data can be transmitted once. By the way, there are many types that can be passed through soap. If post or get is used alone, the data transmission type is relatively simple and the application is complicated, however, next time I plan to write an example that uses ASP, vbs, and XMLHTTP to apply web services.
Before using the example, make sure that you have installed it. NET framework also includes sqlserver2000 and its default example database. In the example, modify the connection code of the database to the code suitable for your database environment, such as the account and password.
Open vs.net, create a new Asp.net (C #) solution Wawa, and add a Web Service: wawaservice. asmx
Import another namespace based on the original one and use the following statement:
Using system. Data. sqlclient;
Add a public method wawa_getdataset. This method is to call the getdataset method and return a dataset. Finally, make sure the code is as follows:
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
Initializecomponent ();
}
# Code generated by the region component designer
// Required by the Web Service designer
Private icontainer components = NULL;
Private void initializecomponent ()
{
}
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}
# Endregion
}
}
In this case, open http: // 192.168.0.110/Wawa/wawaservice. asmx and test it. (Note that the IP address is replaced with the IP address of your own host. If the IP address is unavailable, localhost is used)
Now we use the wsdl.exe command to generate a proxy for this web service in the container. This command should be available after the. NET Framework is installed. In the command mode of the system, enter the following information:
(Note: if you cannot run WSDL directly in cmd command mode, search for this file and configure its path to the PATH variable of the system variable of the server's environment variable, note that the paths are separated by semicolons (;). If you do not understand the path, run Visual Studio directly. NET 2003 command prompt "the tool can run the WSDL command, the following CSC command is the same, someone said:" Visual Studio. NET 2003 command prompt "Where is it? I do.
WSDL/language: CS/namespace: Wawa/out: wawaproxy. CS http: // 192.168.0.110/Wawa/wawaservice. asmx? WSDL
The displayed information is similar to the following:
E:/ME/web.net/wawa> WSDL/language: CS/namespace: Wawa/out: wawaproxy. CS http: // 19
20170.110/Wawa/wawaservice. asmx? WSDL
Microsoft (r) Web Service Description Language Utility
[Microsoft (R). Net Framework, version 1.1.4322.573]
Copyright (c) Microsoft Corporation 1998-2002. All rights reserved.
The file "wawaproxy. cs" is being written ".
E:/ME/web.net/wawa>
Use NotePad to open the generated wawaproxy. CS file. observe the following:
//------------------------------------------------------------------------------
// <Autogenerated>
// This code was generated by a tool.
// Runtime version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be lost if
// The code is regenerated.
/// </Autogenerated>
//------------------------------------------------------------------------------
//
// The 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;
/// <Remarks/>
[System. Diagnostics. debuggerstepthroughattribute ()]
[System. componentmodel. designercategoryattribute ("Code")]
[System. Web. Services. webservicebindingattribute (name = "Wawa servicesoap", namespace = "Wawa")]
Public class wawaservice: system. Web. Services. Protocols. soaphttpclientprotocol {
/// <Remarks/>
Public wawaservice (){
This. url = "http: // 192.168.0.110/Wawa/wawaservice. asmx ";
}
/// <Remarks/>
[System. web. services. protocols. soapdocumentmethodattribute ("Wawa/wawa_getdataset", requestnamespace = "Wawa", responsenamespace = "Wawa", use = system. web. services. description. soapbindinguse. literal, parameterstyle = system. web. services. protocols. soapparameterstyle. wrapped)]
Public System. Data. dataset wawa_getdataset (){
Object [] Results = This. Invoke ("wawa_getdataset", new object [0]);
Return (system. Data. dataset) (results [0]);
}
/// <Remarks/>
Public System. iasyncresult beginwawa_getdataset (system. asynccallback callback, object asyncstate ){
Return this. begininvoke ("wawa_getdataset", new object [0], callback, asyncstate );
}
/// <Remarks/>
Public System. Data. dataset endwawa_getdataset (system. iasyncresult asyncresult ){
Object [] Results = This. endinvoke (asyncresult );
Return (system. Data. dataset) (results [0]);
}
}
}
In this case, we need to compile the wawaproxy. CS file into a dynamic link library and use the following command:
CSC/T: Library/R: system. Web. Services. dll/out: wawaproxy. dll wawaproxy. CS
The prompt is similar to the following:
E:/ME/web.net/wawa> CSC/T: Library/R: system. Web. Services. dll/out: wawaproxy. dll
Wawaproxy. CS
Microsoft (r) Visual C #. Net compiler version 7.10.3052.4
Used for Microsoft (R). Net Framework Version 1.1.4322
Copyright (c) Microsoft Corporation 2001-2002. All rights reserved.
Copy the generated wawaproxy. DLL to the bin directory under the current directory.
Add a wawaservice. aspx form to call the web service and display data. Then, import two namespaces in the background encoding,
Using system. Web. Services;
Using system. Data. sqlclient;
Drag a DataGrid to obtain the name "datagridwawa ".
Ensure that the page is similar to the following:
<% @ Page Language = "C #" codebehind = "wawaservice. aspx. cs" autoeventwireup = "false" inherits = "Wawa. wawaservice1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> wawaservice </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: DataGrid id = "maid" style = "Z-INDEX: 101; left: 168px; position: absolute; top: 232px"
Runat = "server"> </ASP: DataGrid>
</Form>
</Body>
</Html>
Open the background encoding file wawaservice. aspx in the wawaservice. aspx. CS, reference the Web service in the page_load method, and call the dataset returned by the Web service to bind the datagridwawa
After writing, the code is similar to the following:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Web. Services;
Using system. Data. sqlclient;
Namespace Wawa
{
Public class wawaservice1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagridwawa;
Private void page_load (Object sender, system. eventargs E)
{
Wawaservice servicewawa = new wawaservice ();
String str_wawa_ SQL = "select employeeid, titleofcourtesy, firstname, lastname from employees ";
Dataset dswawa = new dataset ();
Dswawa = servicewawa. wawa_getdataset (str_wawa_ SQL );
Datagridwawa. datasource = dswawa;
Datagridwawa. databind ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
Now, set wawaservice. aspx to the start page, CTRL + shilt + B, and generate a solution,
Open http: // 192.168.0.110/Wawa/wawaservice. aspx (of course, the specific address depends on the IP address of your machine) and you will see the desired effect,