Frog Recommendation: A simple example of passing DataSet with a web service

zhaozj2021-02-12  153

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 code was generated by a Tool.//runtime version: 1.1.4322.573/// Changes to this file may cause increct behavior and will be lost.///> code is regenerated.// / / --------------------------------------------------- -----------------------------

// // 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; /// [System .Diagnostics.DebuggerStepThroughAttribute ()] [System.ComponentModel.DesignerCategoryAttribute ( "code")] [System.Web.Services.WebServiceBindingAttribute (Name = "wawa ServiceSoap", Namespace = "wawa")] public class wawaService: System.Web. Services.protocols.soaphttpclientProtocol {/// public wawasemp () {this.url = "http://192.168.0.110/wawa/wawaservice.asmx";} /// [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);} /// PUBLIC System.IAsyncResult Beginwawa_getDataSet (System.AsyncCallback callback, object asyncState) {return this.BeginInvoke ( "wawa_getDataSet", new object [0], callback, asyncState);} /// public System.Data.DataSet Endwawa_getDataSet ( System.iasyncResult asyncRESULT) {Object [] Results = this.endinvoke (asyncRESULT);

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"%> 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"> <ask: dataGrid ID = "DataGridwawa" style = "z-index: 101; left: 168px; position: absolute; top: 232px" runat = "server"> </ asp: dataGrid> </ form> </ body> </ html> Now open the background code file wawaservice.aspx.cs of WAWASERVICE.ASPX, reference the web service in the Page_Load method, and call the DataSet bind DA returned by Web service. After finished taGridwawa code 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; use system.Web.ui.webcontrols; using system.web.ui.htmlcontrols; using system.web.services; use system.data.sqlclient; namespace wawa {</p> <p>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 ();} #region Web form Designer generated Code Override Protected Void Oninit (Eventargs E) {</p> <p>InitializationComponent (); base.onit (e);</p> <p>Private void initializecomponent () {this.load = new system.eventhandler (this.page_load);</p> <p>} #ENDREGON}} Ok, now set WAWASERVICE.ASPX to the start page, generate Ctrl Shilt B, generate a solution, open http://192.168.0.110/wawa/wawasempox (of course, specific address To see your machine's IP) Address, you will see the effect you want,</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6322.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="6322" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.039</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'gkpfI5QQYTY3YaYQuk67sYp_2FO6H0O67bQw9e5JobypOLnQ7r6GWAisTVXVmokXpHM2Us8vPiBOhXkNBj6yBNyw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>