Develop Web Services Concise Tutorials using vs.net
HBZXF (A good)
http://www.cnblogs.com/hbzxf
Directory Index:
1 Create Web Services using Visual Studio.net
2 call Web Services in Web Forms
3 call Web Services in Windows Forms
The reason why it is called the concise tutorial, is listed in one by one by one, so that beginners can easily get started, for complex or related noun interpretation and service principles, you can refer to the relevant books, here no longer talk.
Section 1: Create Web Services using Visual Studio.net
1. Start Visual Studio.net 2, [File] - [New] - [Project] menu, open the [New Project] dialog, select [Visual C # item] in [Project Type], select [Template] [ ASP.NET Web Services], as shown in Figure 1,
3. Changing the default location is http: // localhost / myService, indicating that this Web Service is located in the MyService subdirectory of the HTTP service. 4. Click OK to create a Web service success. 5, switch to the code attempt, you ask me how to switch, double click on the page 6, let us take a line by line to see the automatically generated code meaning
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.diagnostics;
Using system.Web;
Using system.Web.services;
// A string Using is not explained.
Namespace myService // Establish a namespace, the name taken is the name of our creation
{
///
/// service1's summary description.
///
Public class service1: system.web.services.Webservice
{
Public service1 ()
{
// Codegen: This call is necessary for the ASP.NET Web service designer.
InitializationComponent ();
}
#REGION component designer generated code
/ / Web service designer necessary
Private icontainer Components = NULL;
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
///
Private vidinitiRizeComponent ()
{
}
///
/// Clean all the resources being used.
///
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing && Components! = NULL)
{
Components.dispose ();
}
Base.dispose (Disposing);
}
#ndregion
// Web service example
// HelloWorld () sample service Returns Hello World
// To generate, cancel the following list, then save and generate items
/ / To test this web service, press F5 key
// [WebMethod]
// public string helloworld ()
// {
// Return "Hello World";
//}
}
}
7. You may wish to take a note as a plain word, let us see what it is in the browser. Don't forget, you need to recompile after comment. Enter the address in the browser: http://localhost/myservice/service1.asmx may see 8 shown in Figure 2, click on the page HelloWorld will see the method returns the string for HelloWorld, indicating that the Web service is successful 9, below us Add a custom method to your function, the function returns a DataSet, stored as a table information in the DataSet, and we bind the returned DataSet data to the front desk DataGrid control. 10. Since the operation of the database is involved, we need to establish a connection string for WebServices in Web.config, and initialize the commonly used class objects in the service1.asmx file. Add a database connection string in a web.config file, here we use the SQLServer database:
In initialization database connection in Service1.asmx
1. Add using system.data.sqlclient; pay attention to the semicolon end
2. Define global object variables
Protected SqlConnection Conn;
3. Initialize the connection object and the SQLCONNECTION connection string in the constructor
CONN = New SqlConnection ();
Conn.connectionstring = system.configuration.configurationSettings.appsettings ["connString"];
11. After initialization, start the code of the custom method. Here we assume that the following method functions return data in the NORTHWIND database, the detailed code is as follows. You can copy the code to // web service example above
///
/// Name: getEmployees /// parameter: no
/// Function: Retrieve the information /// return value in the Employees table: DataSet, return the retrieved data set.
///
[WebMethod (EnableSession = true, description = "Retrieves the information in the Employees table.")]
Public Dataset getEmployees ()
{
Try
{
DataSet DS = New Dataset ();
String searchstring = "select * from employees";
SqlDataAdapter Da = New SqldataAdapter (SearchString, Conn);
DA.FILL (DS, "Employees");
Return DS;
}
Catch
{
Return NULL;
}
}
Save the current service1.asmx file, recompile the running to see this WebServices found that the new GetEmployees method is found below HelloWorld. Next, we started filling the data in the returned DataSet to the front desk DataGrid.
For details, please see the second section of the WEB Service in Web Forms.
[Endless]
Note: Not reprint without author license