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

えムナウ の とどけもの

 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 DatasetService1
{
        /// <summary>
        /// Service1 の概要の説明です。
        /// </summary>
        [WebService(Namespace="http://localhost/DatasetService1")]
        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 DataSet DataSetRequest()
                {
                        DataSet1 dataset1 = new DataSet1() ;
                        sqlDataAdapter1.Fill( dataset1,"Customers") ;
                        return dataset1 ;
                }
        }
}
レスポンスで返すXMLです。

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://localhost/DatasetService1">
  <xs:schema id="DataSet1" targetNamespace="http://www.tempuri.org/DataSet1.xsd" xmlns:mstns="http://www.tempuri.org/DataSet1.xsd" xmlns="http://www.tempuri.org/DataSet1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
    <xs:element name="DataSet1" msdata:IsDataSet="true" msdata:Locale="ja-JP">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Customers">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="CustomerID" type="xs:string" />
                <xs:element name="CompanyName" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//mstns:Customers" />
        <xs:field xpath="mstns:CustomerID" />
      </xs:unique>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
      <Customers diffgr:id="Customers1" msdata:rowOrder="0">
        <CustomerID>ALFKI</CustomerID>
        <CompanyName>Alfreds Futterkiste</CompanyName>
      </Customers>
      <Customers diffgr:id="Customers2" msdata:rowOrder="1">
        <CustomerID>ANATR</CustomerID>
        <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
      </Customers>
      <Customers diffgr:id="Customers3" msdata:rowOrder="2">
        <CustomerID>ANTON</CustomerID>
        <CompanyName>Antonio Moreno Taqueria</CompanyName>
      </Customers>
      <Customers diffgr:id="Customers4" msdata:rowOrder="3">
        <CustomerID>AROUT</CustomerID>
        <CompanyName>Around the Horn</CompanyName>
      </Customers>
    </DataSet1>
  </diffgr:diffgram>
</DataSet>
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/DatasetService1" 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/DatasetService1" xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://localhost/DatasetService1">
      <s:import namespace="http://www.w3.org/2001/XMLSchema" />
      <s:element name="DataSetRequest">
        <s:complexType />
      </s:element>
      <s:element name="DataSetRequestResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="DataSetRequestResult">
              <s:complexType>
                <s:sequence>
                  <s:element ref="s:schema" />
                  <s:any />
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
    </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/DatasetService1/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/DatasetService1/Service1.asmx" />
    </port>
  </service>
</definitions>