1. 程式人生 > >webService--返回資料集

webService--返回資料集

//Service.cs

using System;                                   //引用System名稱空間下的類

using System.Web;                               //引用Web名稱空間下的類

using System.Web.Services;                      //引用Services名稱空間下的類

using System.Web.Services.Protocols;            //引用Protocols名稱空間下的類

using System.Data.SqlClient;                    //引用SqlClient名稱空間下的類

using System.Collections;                       //引用Collections名稱空間下的類

using System.Data;                              //引用Data名稱空間下的類

[WebService(Namespace = "http://tempuri.org/")]//預設的名稱空間

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service ()                           //預設建構函式

    {

    }

    public class Employee                       //自定義類,用以儲存資料

    {

        public string employeeId;

        public string firstName;

        public string lastName;

    }

     [WebMethod]

     [System.Xml.Serialization.XmlInclude(typeof(Employee))]    //宣告“Employee”類可寫入XML

    public ArrayList GetData()                      //獲得資料庫資料

    {

        SqlConnection conn = new SqlConnection(); //定義“SqlConnnection”類例項

        //資料庫連線字串

        conn.ConnectionString = "Data Source=.;Initial Catalog=Company; Persist Security Info=True;User ID=sa;Password=sa";

        //定義“SqlCommand”例項,從“Employee”表中取資料

        SqlCommand command = new SqlCommand( "select * from Employee",conn);

        conn.Open();                                //開啟連線

        SqlDataAdapter da = new SqlDataAdapter();   //定義“SqlDataAdapter”類例項

        //將“command”值傳遞給“SqlDataAdapter”的“SelectCommand”屬性

        da.SelectCommand = command;

        DataSet ds = new DataSet();                 //定義“DataSet”類例項

        da.Fill(ds, "tables");                      //取資料

        ArrayList al = new ArrayList();             //定義“ArrayList”類例項

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)   //將全部資料儲存於al變數中

        {

            Employee em = new Employee();               //定義“Employee”類例項

            //新增資料到“al”變數中

              em.employeeId= ds.Tables[0].Rows[i]["employeeId"].ToString().Trim();

              em.firstName = ds.Tables[0].Rows[i]["firstName"].ToString().Trim();

              em.lastName=ds.Tables[0].Rows[i]["lastName"].ToString().Trim();

            al.Add(em);

        }

        conn.Close();                                   //關閉資料庫

        return al;

    }

}

(18)       引用名稱空間、類宣告、預設建構函式等程式碼都是VS 2005自動生成的。

(19)       “[WebService(Namespace = "http://tempuri.org/")]”表示WebService的名稱空間。

(20)       [WebMethod]關鍵字指明此函式為WebService的方法。

(21)       DataSet型別是一種多表資料型別。若資料以此型別返回,Flex程式將很難處理。所以本程式中自定義了Employee類,將資料全部儲存於ArrayList型別變數中。

(22)       由於Employee類是自定義的,WebService在生成WSDL(WebService標準檔案)時並不認識,所以需要宣告。宣告語句為“[System.Xml.Serialization.XmlInclude (typeof(Employee))]”。

(23)       資料庫連線語句“Data Source=.;Initial Catalog=Company;Persist Security Info=True; User ID=sa;Password=sa”必須正確。使用者可根據實際設定修改使用者名稱和密碼。

(24)       獲取Employee表全部資料的SQL語句為“select * from Employee”。

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/spyking945/archive/2009/07/07/4325303.aspx


相關推薦

webService--返回資料

//Service.csusing System;                                   //引用System名稱空間下的類 using System.Web;                               //引用Web名稱空間

webservice 返回資料的四種方法

  在使用WebService進行遠端資料操作時,細心的你會發現WebServices的效能特別的慢,當然也曾聽見很多網友也如此如何如何。說實話,WebServices的確比呼叫本地資料要慢一些,可究竟有多慢?真的如網友們說的那麼難以忍受嗎?我個人感覺,多半原因在處理的方式上。讓我們親自編

oracle使用儲存過程返回資料

很多時候,我們想通過儲存過程獲得一個輸出集。我們知道sql server的儲存過程在執行之後,返回的就是一個集合。但是oracle如果要獲得一個輸出集合,就要麻煩一點了。     oracle獲得輸出集合是通過遊標實現的,而且遊標需要在package中進行宣告。下面就拿分頁的

PostgreSQL函式如何返回資料

以下主要介紹PostgreSQL函式/儲存過程返回資料集,或者也叫結果集的示例。 背景: PostgreSQL裡面沒有儲存過程,只有函式,其他資料庫裡的這兩個物件在PG裡都叫函式。 函式由函式頭,體和語言所組成,函式頭主要是函式的定義,變數的定義等,函式體主要是函式的實現

vb 呼叫 Oracle 函式返回資料的例子

PL/SQL 程式碼:CREATE OR REPLACE PACKAGE "SCOTT"."PKG_TEST" AS       TYPE myrcType IS REF CURSOR;       FUNCTION get(strbarcode VARCHAR) RETUR

如何在Delphi中呼叫oracle的儲存過程返回資料

::::::本文的相關評價及說明資訊:::::: 【delphi+oracle報表解決方案(一)】delphi中呼叫oracle的儲存過程(分帶返回遊標,不返回值兩種)  關鍵字: delphi ,oracle儲存過程,遊標,返回資料集,報表 注:delphi 6+ oracle 8.1.6 一.建立包與

獲取webservice 返回的 dataset 資料 並轉換成 datatable

 Service1 service= new Service1(); DataTable dt = service.Get_Data(prods_no.Text.Trim()).Tables[0];             total_pack_amt.Text

delphi 中如何呼叫webservice返回dataset 資料

We're nearly there. Drop a TClientDataset, a TXMLTransformProvider and a TDatasource on the form. Here's what the form looks like now:Link

java HttpClient 訪問webservice並解析返回資料

關於webservice的普及就不多說了,直接進入主題吧。 1.導包 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient HttpClient相關包-->

Mybatis 返回Map & List動態列資料

 1、xml檔案中的resultType都指定為HashMap: <select id="selectListMap" parameterType="java.lang.String" resultType="java.util.HashMap">

oracle pipelined返回值函式 針對資料彙總統計 返回結果方法

/*開啟日誌輸出*/ Set serveroutput on ; /*建立型別*/ create or replace type type_flux_data_stat_o as object ( ifinoctetsbps number , ifoutoctetsbps number

android利用ksoap2返回複雜資料資料(dataset)

在讀這篇文章之前建議你先讀一下上一篇文章android如何使用ksoap2對sql server的操作實現登陸,原理是一樣的只是返回的資料不同而已。 web端程式碼: //database.cs檔案利用ADO.NET技術 public DataSet G

WebService 返回json格式和返回xml格式的資料

返回json格式 //using System.Web.Script.Services; [WebMethod] [ScriptMethod(UseHttpGet =

Java接收Cordys中webservice介面的返回資料並解析xml獲取相應節點資料

在做專案的過程中,需要用Java呼叫Cordys的webservice介面的返回資料,眾所周知,webservice返回的資料是xml形式的,那麼我們怎樣獲取相關節點下的資料呢? 處理之前返回的資料格式如下: <soap:Envelope xmln

webservice(第三天)(包含一個真實專案,讀取EXCEL表格中的資料到資料庫中,其中涉及真實的隱私資料不能公開資料)

主要內容: CXF的介紹、安裝和配置 CXF釋出SOAP協議的服務 CXF+Spring整合釋出SOAP的服務 CXF釋出REST服務 什麼是REST CXF+Spring整合釋出REST服務 綜合案例 CXF介紹: cxf是一個開源的webservice框架,提供很多完善

使用webservice返回xml格式資料使用jq解析

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

WebService返回文字JSON資料格式

WebService返回的格式都是xmlMarkup<?xml version="1.0" encoding="utf-8"?><string xmlns="">Hello World</string> 在前段js處理時需要先解析xmlJa

Webservice返回json資料格式不帶xml頭部

我將結果內容用字串拼接成Json資料並返回的時候,會在結果前面新增xml頭部,結果如下。 <span ><string xmlns="http://tempuri.org/">   {"data":[{"batchId":"B001","pro

java向webService介面(.net)傳送請求並接收返回資料

​ import java.io.BufferedReader; import java.io.IOException; i

js 返回

color lac slice ret 個數 function col while循環 匹配 //接受兩個數組,返回差集 function getDiffSet(a,b){ a.sort(charSort); b.sort(charSort); i