Using Microsoft WebService technology to achieve remote data inventory, Li Lingyu 01-9-7 01:18:14
With the release of the Microsoft Visual Studo.net beta version, due to the power of Visual Studio.net for XML and Web services, it will be more and more convenient to use Visual Studio.NET development web service applications. This article uses a B2B e-commerce website as an example to introduce the specific methods and steps of sharing the same database using web services to share the same database between different sites. In this article, the client refers to the party that uses the web service, the server side refers to the other party that provides a web service.
statement of problem
The site is an e-commerce website that is engaged in online sales of SIM cards online. Not long ago, the site works with another website (referred to as b) to jointly carry out online sales Unicom mobile SIM card business. Because of the number of A sites used, access is a database of A sites, so I use WebService technology to develop online card system for another website.
Modules and key code for each major function
1. The database uses SQL Server2000 to display the paging display using the stored procedure implementation number. code show as below:
CREATE Procedure Fenye
(
@PAGEnow Int,
@PageSize Int,
@cityid int,
@code char (3),
@Recordcount Int Output
)
AS
Set nocount on
Declare @allid int, @ beginger int, @ Endid Int, @ pagebegin char (11), @ pageend char (11)
SELECT @ allid = count (*) from jinan where cityid = @ Cityid and (code like @code '%')
SELECT @ recordcount = @ allid
Declare Cur_fastRead Cursor Scroll for
Select code from jinan where cityid = @ cityid and (code like @code '%') Order by code
Open Cur_fastRead
SELECT @beginid = (@ Pagenow-1) * @ Pagesize 1
SELECT @ endid = @ beginge @ Pagesize-1
Fetch absolute @beginid from cur_fastread @pagebegin
IF @endid> @allid
Fetch last from cur_fastread @pagend
Else
Fetch absolute @endid from cur_fastread @pagend
Set nocount off
SELECT CODE, COST, STATUS from Jinan Join xuanhaofei on jinan.category = xuanhaofei.category and jinan.cityid = xuanhaofei.cityID
Where code between @PageBegin and @PageEnd Order by Code
Close Cur_fastRead
Deallocate Cur_fastRead
Go
2. Create WebService with Visual Studio.net. In Visual Studo.net, the WebService file extension is .asmx. This file is placed on a website for other websites. * Start Visual Studio.net, select New Project.
* Select Visual C # Projects in the left version, select ASP.NET WebService in the right.
* Click the OK button to generate a WebService project. Create a new webservice file in the project, WebService1.asmx. This file implements access to the database and outputs a string for the caller.
Below is the code of this file:
WebService1.asmx.cs
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.data.sqlclient;
Using system.configuration;
Using system.text;
Using system.diagnostics;
Using system.Web;
Using system.Web.services;
Namespace WebService1
{
Public Class Service2: System.Web.Services.WebService
{
SqlConnection CON;
Public service2 ()
{
// Codegen: this call is required by the ASP.NET Web Services Designer
InitializationComponent ();
}
[WebMethod] // [WebMethod] Property declares that this method can be called by remote user as a web service
Public String Table (int pagenow, int cityid)
{
INT recordcount; // total number
INT Page = 0; // Total number
INT j = 0;
SqlDataReader D = getCode (Pagenow, CityID, Out RecordCount);
IF (RecordCount% 39 == 0)
{
Page = RecordCount / 39; / / only 39 numbers per page
}
Else
{
Page = RecordCount / 39 1;
}
StringBuilder str = new StringBuilder ( "
");
Str.Append (" Str.Append (" selection fee font> td> } Str.Append (" TR> While (D.Read ()) { Str.Append (" Str.Append (D ["Code"]. TOSTRING ()); Str.Append (" td> Str.Append (D ["COST"]. TOSTRING ()); Str.Append (" td>"); IF ((D ["status"]. TOSTRING (). Trimend ()) == "Has been scheduled") { Str.Append (" Str.Append (""); Str.Append (" td>"); } Else { Str.Append (" Str.Append ("
Str.Append (D ["Code"]. TOSTRING ()); Str.Append (") 'width = '24' height = '24 '>"); Str.Append (" td>"); } J ; IF (j% 3 == 0) { Str.Append (" TR> } D. close (); C. close (); Str.Append (" TR> Table> IF (PageNow == 1) { Str.Append (" Home Previous font>"); } Else { Str.Append (" Home "); Str.Append (" Previous font>"); } IF (Pagenow == Page) { Str.Append (" Next page Last" font> "); } Else { Str.Append (" Next Page Tissue font>); } Str.Append (" Page: font> ); Str.Append (PageNow); Str.Append (" font> /"); Str.Append (page); Str.Append (" Strong> page font>"); Str.Append (" A total of "); Str.Append (RecordCount); Str.Append (" b> number 39 b> number / page font>"); Return str.tostring (); } Private SqlDataReader getCode (int Pagenow, int cityid, out int recordcount) { SqlDataReader Dr = NULL; Con = New SqlConnection ("Server = localhost; database = yitong; uid = sa; pwd ="); SQLCommand cmd = new SQLCOMMAND ("Fenye", Con); cmd.commandtype = commandtype.storedProcedure; Cmd.Parameters.Add (New Sqlparameter ("@ Pagenow", SqldbType.Int); cmd.parameters ["@ Pagenow"]. value = PageNow; // Currently located page Cmd.Parameters.Add (New Sqlparameter ("@ pagesize", sqldbtype.int); cmd.parameters ["@ Pagesize"]. Value = 39; / / Number of numbers to display per page Cmd.Parameters.Add (New Sqlparameter ("@ CityID", SqldbType.InT); cmd.parameters ["@ CityID"]. value = cityid; // City code CMD.Parameters.Add (New Sqlparameter ("@ code", sqldbtype.char, 3)); cmd.Parameters ["@ code"]. value = "130"; // only search for Unicom mobile phone number Sqlparameter q; Q = cmd.Parameters.add (New Sqlparameter ("@ recordcount", sqldbtype.int); q.direction = parameterdirection.output; C.Open (); cmd.executenonquery (); Recordcount = (int) cmd.parameters ["@ recordcount"]. value; // Return number total number DR = cmd.executeReader (); Return DR; } } } 3. The client page is stored on the B website. When a customer browses the site, you can browse, order the A site database. The client page uses Microsoft's WebService Behavior technology to call a web service on A. WebService Behavior is a technology that Microsoft added a new increase in IE5.0 can be used by page scripts. She uses the SOAP protocol to communicate with the web service, which can dynamically update the topical page, not to refresh the entire page, which is more effective than the usual use of the entire page, it is more effective. To use this technology, you must download a webservice.htc component to the directory where the webservice.htc component is located on the Microsoft website. The code of the client page is as follows: Client.htm
Function window_onload () { // Call the web service provided by A Service.useService ("http://ipofa/service1.asmx? wsdl", "myselect"); / / Call the Table method of the web service, display the first page Service.myselect.callService (showcode, "table", 1, city.value); Function city_onchange () { Service.service1.callService (showcode, "table", 1, city.value); } Function AddCart (ID) { URL = "BASKET.ASP? Code =" ID; Window.naviGate (URL); } Function next (x) { // Show next page Service.myselect.callService (showcode, "table", x, city.value) } Function first () { // Display Home Service.myselect.callService (showcode, "Table", 1, City.Value); } Function previous (x) { // Display the previous page Service.Myselect.CallService (showcode, "Table", X, City.Value; } Function last (x) { // Display the last page Service.Myselect.CallService (showcode, "Table", X, City.Value; } Function showcode (result) { // Result Save the result of the call to the Web service Service.innerhtml = Result.Value; } // -> script> hEAD> select> div> body> html> It can be seen that WebService Behavior allows a static page to use a web service through a script, and do not have to create a proxy on the client, just copy a webservice.htc component. With Visual Studio.net, you don't have to know the underlying protocols such as HTTP, XML, SOAP, WSDL, which can also develop and use web services, really so cool. BRIEF DESCRIPTION OF THE DRAWINGS: (This is the page that I am displayed during this machine for reference) |