Use web services in .NET

xiaoxiao2021-03-06  41

Recently, because of the Web module in the project group, start learning the .NET platform, discovering that using a web service in the .NET platform is still very simple. The following is a simple example of creating a web service under the .NET platform. First created with a Visul Studio .Net C # project Asp.Net Web service program, the source 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;

A summary description of Namespace Author {///

/// service1. /// public class service1: system.Web.Services.Webservice {public service1 () {// codegen: This call is the initializeComponent () () necessary for the ASP.NET Web service designer.

#Region component designer generated code // Web service designer essential private icontainer components = null; ///

/// designer supports the required method - Do not use the code editor to modify /// The content of the method. /// private void initializecomponent ()}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (Disposing && Components! = NULL) {components.dispose ();} Base.dispose (Disposing);} #ENDREGION

// Web Service Sample // HelloWorld () Sample Service Returns Hello World // To generate, cancel the following list, then save and generate items // To test this web service, press F5 button

// [WebMethod] // public string helloworld () // {// return "Hello World"; //}

}} These code are automatically generated by the system. From here, it can be seen that the ordinary approach has become a web method after adding the WebMethod property. The following is a method of accessing the SQL Server database. The code is as follows: [WebMethod] Public DataSet DataVisit (String ID) {string myselectQuery = "SELECT AU_ID, AU_FNAME, AU_LNAME FROM AUTHORE AU_ID! = '" ID " "; string myConn = @" server = localhost; uid = sa; database = pubs "; SqlConnection myConnection = new SqlConnection (myConn); SqlCommand myCmd = new SqlCommand (mySelectQuery, myConnection); myConnection.Open (); SqlDataAdapter adapter = new SqldataAdapter (); adapter.selectcommand = mycmd; dataset myds = new dataset (); adapter.fill (myds, "author_name"); myconnection.close (); returnnection;} This created a web service, in Web This service can be used by adding "Web Reference". Use this method to easily create a three-layer mode.

转载请注明原文地址:https://www.9cbs.com/read-71922.html

New Post(0)