Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 » SQL Anywhere Server - Programming » SQL Anywhere Data Access APIs » SQL Anywhere web services


Tutorial: Using data types with Microsoft .NET

The following tutorial demonstrates how to use the SQL Anywhere web service datatype support from within Microsoft .NET using Visual C#.

To create SOAP and DISH services
  1. At a command prompt, run the following command to start a personal web server. Replace samples-dir with the actual location of the sample database. The -xs http(port=80) option tells the database server to accept HTTP requests on port 80. If you already have a web server running on port 80, use another port number such as 8080 for this tutorial.

    dbeng11 -xs http(port=80) samples-dir\demo.db
  2. Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statements:

    1. Define a SOAP service that lists the Employees table.

      CREATE SERVICE "SASoapTest/EmployeeList"
      TYPE 'SOAP'
      USER DBA
      AS SELECT * FROM Employees;

      In this example, DATATYPE OUT is specified to generate datatype information in the XML result set response. A fragment of the response from the web server is shown below. Note that the type information matches the data type of the database columns.

      <xsd:element name='EmployeeID' minOccurs='0' type='xsd:int' />
      <xsd:element name='ManagerID' minOccurs='0' type='xsd:int' />
      <xsd:element name='Surname' minOccurs='0' type='xsd:string' />
      <xsd:element name='GivenName' minOccurs='0' type='xsd:string' />
      <xsd:element name='DepartmentID' minOccurs='0' type='xsd:int' />
      <xsd:element name='Street' minOccurs='0' type='xsd:string' />
      <xsd:element name='City' minOccurs='0' type='xsd:string' />
      <xsd:element name='State' minOccurs='0' type='xsd:string' />
      <xsd:element name='Country' minOccurs='0' type='xsd:string' />
      <xsd:element name='PostalCode' minOccurs='0' type='xsd:string' />
      <xsd:element name='Phone' minOccurs='0' type='xsd:string' />
      <xsd:element name='Status' minOccurs='0' type='xsd:string' />
      <xsd:element name='SocialSecurityNumber' minOccurs='0' type='xsd:string' />
      <xsd:element name='Salary' minOccurs='0' type='xsd:decimal' />
      <xsd:element name='StartDate' minOccurs='0' type='xsd:date' />
      <xsd:element name='TerminationDate' minOccurs='0' type='xsd:date' />
      <xsd:element name='BirthDate' minOccurs='0' type='xsd:date' />
      <xsd:element name='BenefitHealthInsurance' minOccurs='0' type='xsd:boolean' />
      <xsd:element name='BenefitLifeInsurance' minOccurs='0' type='xsd:boolean' />
      <xsd:element name='BenefitDayCare' minOccurs='0' type='xsd:boolean' />
      <xsd:element name='Sex' minOccurs='0' type='xsd:string' />
    2. Create a DISH service to act as a proxy for the SOAP service and to generate the WSDL document.

      TYPE 'DISH'
      GROUP "SASoapTest"
      USER DBA;

      The SOAP and DISH service must be of format DNET. In this example, the FORMAT clause was omitted when the SOAP service was created. As a result, the SOAP service inherits the DNET format from the DISH service.

  3. Start Microsoft Visual Studio. Note that this example uses functions from the .NET Framework 2.0.

    1. Create a new Windows Application project using Visual C#.

      An empty form appears.

    2. From the Project menu, choose Add Web Reference.

    3. In the URL field of the Add Web Reference page, enter the following URL: http://localhost:80/demo/SASoapTest_DNET.

    4. Click Go.

      You are presented with a list of the methods available for SASoapTest_DNET. You should see the EmployeeList method.

    5. Click Add Reference to finish.

      The Solution Explorer window shows the new web reference.

    6. From the Visual Studio Toolbox, add a listbox and a button to the form as shown in the following diagram.

      SOAP demonstration form.
    7. Rename the button text to Employee List.

    8. Double-click Employee List and add the following code for the button click event.

      int sqlCode;
      localhost.SASoapTest_DNET proxy = new localhost.SASoapTest_DNET();
      DataSet results = proxy.EmployeeList(out sqlCode);
      DataTableReader dr = results.CreateDataReader();
      while (dr.Read())
        for (int i = 0; i < dr.FieldCount; i++)
          string columnName = "(" + dr.GetDataTypeName(i) 
                            + ")" 
                            + dr.GetName(i);
          if (dr.IsDBNull(i))
            listBox1.Items.Add(columnName + "=(null)");
          else {
            System.TypeCode typeCode = 
            switch (typeCode)
              case System.TypeCode.Int32:
                Int32 intValue = dr.GetInt32(i);
                listBox1.Items.Add(columnName + "=" 
                    + intValue);
              case System.TypeCode.Decimal:
                Decimal decValue = dr.GetDecimal(i);
                listBox1.Items.Add(columnName + "=" 
                    + decValue.ToString("c"));
              case System.TypeCode.String:
                string stringValue = dr.GetString(i);
                listBox1.Items.Add(columnName + "=" 
                    + stringValue);
              case System.TypeCode.DateTime:
                DateTime dateValue = dr.GetDateTime(i);
                listBox1.Items.Add(columnName + "=" 
                    + dateValue);
              case System.TypeCode.Boolean:
                Boolean boolValue = dr.GetBoolean(i);
                listBox1.Items.Add(columnName + "=" 
                    + boolValue);
              case System.TypeCode.DBNull:
                    + "=(null)");
                    + "=(unsupported)");

      This example is designed to illustrate the fine control the application developer can have over the datatype information that is available.

    9. Build and run the program.

The XML response from the web server includes a formatted result set. The first row of the formatted result set is shown below.

 <Street>9 East Washington Street</Street>
 <TerminationDate xsi:nil="true" />

There are a few things to note about the XML result set response.

The listbox will display the EmployeeList result set as (type)column name=value pairs. The result from processing the first row of the result set is shown below.

(String)Street=9 East Washington Street
(String)State=New York
(DateTime)StartDate=28/08/1984 0:00:00 AM
(DateTime)BirthDate=05/06/1958 0:00:00 AM

There are a couple of things to note about the results.