<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      使用微軟WebService技術完成遠程數據庫存取 使用web服務在不同網站間共享同一數據庫

      [摘要]隨著微軟Visual Studo.Net Beta版的發布,由于Visual Studio.Net對XML以及Web服務的強大支持,利用Visual Studio.Net開發Web服務應用將會越來越...
      隨著微軟Visual Studo.Net Beta版的發布,由于Visual Studio.Net對XML以及Web服務的強大支持,利用Visual Studio.Net開發Web服務應用將會越來越多而且是非常的方便。本文以一個B2B電子商務網站為例,介紹利用web服務在不同站點間共享同一數據庫的具體方法和步驟。本文中,客戶端是指使用web服務的一方,服務器端是指提供web服務的另一方。

      問題的提出

        該網站是一家(簡稱A)從事網上銷售手機SIM卡的業務的電子商務網站。前不久,該網站與另一家網站(簡稱B)合作,共同開展網上銷售聯通手機SIM卡業務。由于都是使用的A網站的號碼資源,存取的都是A網站的數據庫,于是筆者利用webservice技術為另一家網站開發了網上售卡系統。

      各主要功能的模塊和關鍵代碼

      1. 數據庫采用SQL SERVER2000,使用存儲過程實現號碼瀏覽的分頁顯示。代碼如下:
      create procedure fenye
      (
      @pagenow int,
      @pagesize int,
      @cityid int,
      @code char(3),
      @recordcount int output
      )
      as
      set nocount on

      declare @allid int,@beginid 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=@beginid+@pagesize-1

      fetch absolute @beginid from cur_fastread into @pagebegin

      if @endid>@allid
      fetch last from cur_fastread into @pageend
      else
      fetch absolute @endid from cur_fastread into @pageend

      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. 用Visual Studio.net創建webservice。在visual studo.net中,webservice文件的擴展名是.asmx。該文件放在A網站上,供其他網站調用。
      * 啟動visual studio.net,選擇new project。
      * 在左面版中選擇visual c# projects,在右面版中選擇ASP.NET WebService。
      * 單擊ok按鈕就生成了一個webservice項目。在項目中新建一個webservice文件,WebService1.asmx。該文件實現對數據庫的存取,并對調用者輸出一個字符串。

      下面是該文件的代碼:

      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
      InitializeComponent();
      }

      [WebMethod] //[WebMethod]屬性聲明此方法作為web服務可以被遠程使用者調用
      public string table(int pagenow,int cityid)
      {
      int recordcount;//總號碼數
      int page=0;//總頁數
      int j=0;

      SqlDataReader d=GetCode(pagenow,cityid,out recordcount);

      if(recordcount%39==0)
      {
      page=recordcount/39;//每頁只顯示39個號碼

      }
      else
      {
      page=recordcount/39+1;
      }

      StringBuilder str=new StringBuilder("<table border='1' width='100%' bordercolorlight='00008B' bordercolordark='#fffff0' cellspacing='0' cellpadding='0' height='24'><tr>");

      for(int i=0;i<3;i++)
      {
      str.Append("<td bgcolor='#f0f8ff' align='middle' height='0' valign='center'>");
      str.Append("<p style='MARGIN-BOTTOM: 2px'><font size='2'>號 碼</font></p></td>");
      str.Append("<td bgcolor='#f0f8ff' align='middle' height='0' valign='center'>");
      str.Append("<font size='2'>選號費</font></td><td bgcolor='#f0f8ff' align='middle' height='0' valign='center'> </td>");
      }

      str.Append("</tr><tr>");

      while(d.Read())
      {
      str.Append("<td height='24' align='middle'><font size='2'>");
      str.Append(d["code"].ToString());
      str.Append("</td><td height='24' align='middle'><font size='2'> ");
      str.Append(d["cost"].ToString());
      str.Append("</td>");

      if((d["status"].ToString().TrimEnd())=="已預定")
      {
      str.Append("<td height='24' align='middle'>");
      str.Append("<input type='image' name='image' src='http://cfan.net.cn/info/images/hand.gif'>");
      str.Append("</td>");
      }
      else
      {
      str.Append("<td height='24' align='middle'>");
      str.Append("<input type='image' name='image' src='http://cfan.net.cn/info/images/cart.jpg' onclick='javascript:addcart(");
      str.Append(d["code"].ToString());
      str.Append(")' width='24' height='24'>");
      str.Append("</td>");
      }

      j++;
      if(j%3==0)
      {
      str.Append("</tr><tr>");
      }

      }
      d.Close();
      con.Close();

      str.Append("</tr><br>");

      if(pagenow==1)
      {
      str.Append("<font color='#000080' size=2>首頁 上一頁</font> ");
      }
      else
      {
      str.Append("<font color='#000080' size=2><a href='javascript:first()'>首頁</a> ");
      str.Append("<a href='javascript:previous(");
      str.Append(pagenow-1);
      str.Append(")'>上一頁</a></font> ");
      }

      if(pagenow==page)
      {
      str.Append("<font color='#000080' size=2>下一頁 尾頁</font>");
      }
      else
      {
      str.Append("<a href='javascript:next(");
      str.Append(pagenow+1);
      str.Append(")'><font color='#000080' size=2>下一頁</a> <a href='javascript:last(");
      str.Append(page);
      str.Append(")'>尾頁</a></font>");
      }
      str.Append("<font color='#000080' size=2> 頁次:</font><strong><font color=red size=2>");
      str.Append(pagenow);
      str.Append("</font><font color='#000080' size=2>/");
      str.Append(page);
      str.Append("</strong>頁</font>");
      str.Append("<font color='#000080' size=2> 共<b>");
      str.Append(recordcount);
      str.Append("</b>個號碼 <b>39</b>個號碼/頁</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;//目前所在頁面

      cmd.Parameters.Add(new SqlParameter("@pagesize",SqlDbType.Int));
      cmd.Parameters["@pagesize"].Value=39;//每頁要顯示的號碼數

      cmd.Parameters.Add(new SqlParameter("@cityid",SqlDbType.Int));
      cmd.Parameters["@cityid"].Value=cityid;//城市代碼

      cmd.Parameters.Add(new SqlParameter("@code",SqlDbType.Char,3));
      cmd.Parameters["@code"].Value="130";//只搜索聯通的手機號碼

      SqlParameter q;
      q=cmd.Parameters.Add(new SqlParameter("@recordcount",SqlDbType.Int));
      q.Direction=ParameterDirection.Output;

      con.Open();

      cmd.ExecuteNonQuery();
      recordcount=(int)cmd.Parameters["@recordcount"].Value;//返回的號碼總數

      dr=cmd.ExecuteReader();

      return dr;
      }
      }
      }

      3. 客戶端頁面存放在B網站上。當客戶瀏覽該網站時,通過該頁面可以瀏覽、訂購A網站數據庫中號碼?蛻舳隧撁媸褂梦④浀腤ebservice Behavior技術調用A上的web服務。WebService Behavior是微軟在IE5.0中新增加的一項可以通過頁面腳本使用web服務的技術。她使用SOAP協議與web服務通訊,可以動態更新頁面的局部,而不刷新整個頁面,較之通常使用的整頁刷新的方法,它更快也更有效。要使用這項技術,須從微軟網站上下載一個webservice.htc組件到客戶端頁面所在的目錄下。

      客戶端頁面的代碼如下:
      Client.htm

      <html>
      <head>
      <title></title>
      <script language=”javascript”>
      <!--
      function window_onload() {
      //調用A提供的web服務
      service.useService("http://IPofA/service1.asmx?WSDL","myselect");
      //調用web服務的table方法,顯示第一個頁面
      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)
      {
      //顯示下一頁
      service.myselect.callService(showCode,"table",x,city.value)
      }

      function first()
      {
      //顯示首頁
      service.myselect.callService(showCode,"table",1,city.value);
      }

      function previous(x)
      {
      //顯示上一頁
      service.myselect.callService(showCode,"table",x,city.value);
      }

      function last(x)
      {
      //顯示最后一頁
      service.myselect.callService(showCode,"table",x,city.value);
      }

      function showCode(result)
      {
      //result保存調用web服務后返回的結果
      service.innerHTML=result.value;
      }

      //-->
      </script>
      </head>
      <body onload="return window_onload()">
      <select language="javascript" name="city" onchange="return city_onchange()">
      <option value="531" selected> 山東濟南</option>
      <option value="537"> 山東濟寧</option> <option value="546"> 山東東營</option>
      </select>
      <div id="service" style="behavior:url(webservice.htc)">
      </div>
      </body>
      </html>
        可以看到,webservice behavior可以使一個靜態頁面通過腳本程序使用web服務,而且不用在客戶端建立代理,只要拷貝一個webservice.htc組件就可以了。
        利用Visual Studio.Net,你可以不必了解HTTP、XML、SOAP、WSDL等底層協議,同樣能開發和使用Web服務,真得是好爽。


      附圖:(這是我在本機調試時顯示的頁面,供參考)



      日韩精品一区二区三区高清