えムナウのプログラミングのページ

えムナウ の とどけもの

 Logo えムナウBlog@AILight
えムナウBlog@Wankuma

目次

利用状況

イメージ ギャラリー

カテゴリ

Links
 

inetaj

MSMVPロゴ
MSMVP Visual C# 2005/01-2007/12
データセットの代わりに構造体配列をWebサービスで返す。

プログラムです。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace DatasetService2
{
        /// <summary>
        /// Service1 の概要の説明です。
        /// </summary>
        [WebService(Namespace="http://localhost/DatasetService2")]
        public class Service1 : System.Web.Services.WebService
        {
                public Service1()
                {
                        //CODEGEN: この呼び出しは、ASP.NET Web サービス デザイナで必要です。
                        InitializeComponent();
                }

                private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
                private System.Data.SqlClient.SqlConnection sqlConnection1;
                private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;

                #region コンポーネント デザイナで生成されたコード 
                
                //Web サービス デザイナで必要です。
                private IContainer components = null;
                                
                /// <summary>
                /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
                /// コード エディタで変更しないでください。
                /// </summary>
                private void InitializeComponent()
                {
                        this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
                        this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
                        this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
                        // 
                        // sqlSelectCommand1
                        // 
                        this.sqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName FROM Customers";
                        this.sqlSelectCommand1.Connection = this.sqlConnection1;
                        // 
                        // sqlConnection1
                        // 
                        this.sqlConnection1.ConnectionString = "workstation id=\"(local)\";packet size=4096;integrated security=SSPI;data source=\"(" +
                                "local)\";persist security info=False;initial catalog=pubs";
                        // 
                        // sqlDataAdapter1
                        // 
                        this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
                        this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] {
                                        new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
                                        new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName")})});
                }

                /// <summary>
                /// 使用されているリソースに後処理を実行します。
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if(disposing && components != null)
                        {
                                components.Dispose();
                        }
                        base.Dispose(disposing);                
                }

                #endregion

                [WebMethod]
                public Customers[] DataSetRequest()
                {
                        DataSet1 dataset1 = new DataSet1() ;
                        sqlDataAdapter1.Fill( dataset1, "Customers" ) ;
                        Customers[] customers = new Customers[dataset1.Customers.Count] ;
                        for ( int i=0 ; i<dataset1.Customers.Count ; i++ )
                        {
                                customers[i] = new Customers( 
                                        dataset1.Customers[i].CustomerID,
                                        dataset1.Customers[i].CompanyName ) ;
                        }
                        return customers ;
                }

                public struct Customers
                {
                        public string CustomerID ;
                        public string CompanyName ;
                        public Customers( string customerID, string companyName )
                        {
                                CustomerID = customerID ;
                                CompanyName = companyName ;
                        }
                }
        }
}
レスポンスで返すXMLです。

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomers xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost/DatasetService2">
  <Customers>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
  </Customers>
  <Customers>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
  </Customers>
  <Customers>
    <CustomerID>ANTON</CustomerID>
    <CompanyName>Antonio Moreno Taqueria</CompanyName>
  </Customers>
  <Customers>
    <CustomerID>AROUT</CustomerID>
    <CompanyName>Around the Horn</CompanyName>
  </Customers>
</ArrayOfCustomers>
WSDLです。

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://localhost/DatasetService2" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" targetNamespace="http://localhost/DatasetService2" xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://localhost/DatasetService2">
      <s:element name="DataSetRequest">
        <s:complexType />
      </s:element>
      <s:element name="DataSetRequestResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="DataSetRequestResult" type="s0:ArrayOfCustomers" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfCustomers">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="Customers" type="s0:Customers" />
        </s:sequence>
      </s:complexType>
      <s:complexType name="Customers">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="CustomerID" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="CompanyName" type="s:string" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </types>
  <message name="DataSetRequestSoapIn">
    <part name="parameters" element="s0:DataSetRequest" />
  </message>
  <message name="DataSetRequestSoapOut">
    <part name="parameters" element="s0:DataSetRequestResponse" />
  </message>
  <portType name="Service1Soap">
    <operation name="DataSetRequest">
      <input message="s0:DataSetRequestSoapIn" />
      <output message="s0:DataSetRequestSoapOut" />
    </operation>
  </portType>
  <binding name="Service1Soap" type="s0:Service1Soap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <operation name="DataSetRequest">
      <soap:operation soapAction="http://localhost/DatasetService2/DataSetRequest" style="document" />
      <input>
        <soap:body use="literal" />
      </input>
      <output>
        <soap:body use="literal" />
      </output>
    </operation>
  </binding>
  <service name="Service1">
    <port name="Service1Soap" binding="s0:Service1Soap">
      <soap:address location="http://localhost/DatasetService2/Service1.asmx" />
    </port>
  </service>
</definitions>