Using Microsoft WebService technology to achieve remote data inventory to use web services to share the same database between di

zhaozj2021-02-08  210

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 ( "

");

For (int i = 0; i <3; i )

{

Str.Append ("

");

While (D.Read ())

{

Str.Append ("

");}

}

D. close ();

C. close ();

Str.Append ("
");

IF (PageNow == 1)

{

Str.Append (" Home Previous ");

}

Else

{

Str.Append (" Home ");

Str.Append (" Previous ");

}

IF (Pagenow == Page)

{

Str.Append (" Next page Last" ");

}

Else

{

Str.Append (" Next Page Tissue );

}

Str.Append (" Page: );

Str.Append (PageNow);

Str.Append (" /");

Str.Append (page);

Str.Append (" page ");

Str.Append (" A total of ");

Str.Append (RecordCount);

Str.Append (" number 39 number / page ");

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

</ title></p> <p><script language = "javascript"></p> <p><! -</p> <p>Function window_onload () {</p> <p>// Call the web service provided by A</p> <p>Service.useService ("http://ipofa/service1.asmx? wsdl", "myselect");</p> <p>/ / Call the Table method of the web service, display the first page</p> <p>Service.myselect.callService (showcode, "table", 1, city.value);</p> <p>Function city_onchange () {</p> <p>Service.service1.callService (showcode, "table", 1, city.value);</p> <p>}</p> <p>Function AddCart (ID)</p> <p>{</p> <p>URL = "BASKET.ASP? Code =" ID;</p> <p>Window.naviGate (URL);</p> <p>}</p> <p>Function next (x)</p> <p>{</p> <p>// Show next page</p> <p>Service.myselect.callService (showcode, "table", x, city.value)</p> <p>}</p> <p>Function first ()</p> <p>{</p> <p>// Display Home</p> <p>Service.myselect.callService (showcode, "Table", 1, City.Value);</p> <p>}</p> <p>Function previous (x)</p> <p>{</p> <p>// Display the previous page</p> <p>Service.Myselect.CallService (showcode, "Table", X, City.Value;</p> <p>}</p> <p>Function last (x)</p> <p>{</p> <p>// Display the last page</p> <p>Service.Myselect.CallService (showcode, "Table", X, City.Value;</p> <p>}</p> <p>Function showcode (result)</p> <p>{</p> <p>// Result Save the result of the call to the Web service</p> <p>Service.innerhtml = Result.Value;</p> <p>}</p> <p>// -></p> <p></ script></p> <p></ hEAD></p> <p><body οnlοad = "Return window_onload ()"></p> <p><select language = "javaScript" name = "city" οnchange = "Return City_onchange ()"></p> <p><option value = "531" SELECTED> Shandong Jinan </ option></p> <p><option value = "537"> Shandong Jining </ option> <option value = "546"> Shandong Dongying </ Option></p> <p></ select></p> <p><div id = "service" style = "behavior: URL (WebService.htc)"></p> <p></ div></p> <p></ body></p> <p></ html></p> <p>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.</p> <p>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.</p> <p>BRIEF DESCRIPTION OF THE DRAWINGS: (This is the page that I am displayed during this machine for reference)</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-3335.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="3335" 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.056</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 = 'bze83TaO4S29BJvFSWvJ6pp7d3BqxR86uwOmn2TvnKRc2FKOgu5i3rSJEOOmJn_2FfnIQnUixc86oZHftNhei2bQ_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>

");

Str.Append (" number "); str.append ("

");

Str.Append (" selection fee

");

}

Str.Append ("

);

Str.Append (D ["Code"]. TOSTRING ());

Str.Append ("

");

Str.Append (D ["COST"]. TOSTRING ());

Str.Append ("");

IF ((D ["status"]. TOSTRING (). Trimend ()) == "Has been scheduled")

{

Str.Append ("

");

Str.Append ("");

Str.Append ("");

}

Else

{

Str.Append ("

");

Str.Append ("");

Str.Append ("");

}

J ;

IF (j% 3 == 0)

{

Str.Append ("