The following tutorial demonstrates how to access web services from Microsoft .NET using Visual C#.
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 and use 8080 instead of 80 in all port references.
dbeng11 -xs http(port=80) samples-dir\demo.db |
Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statements:
Define a SOAP service that lists the Employees table.
CREATE SERVICE "SASoapTest/EmployeeList" TYPE 'SOAP' AUTHORIZATION OFF SECURE OFF USER DBA AS SELECT * FROM Employees; |
Because authorization has been turned off, anyone can use this service without supplying a user name and password. The commands run under user DBA. This arrangement is simple, but insecure.
Create a DISH service to act as a proxy for the SOAP service and to generate the WSDL document.
CREATE SERVICE "SASoapTest_DNET" TYPE 'DISH' GROUP "SASoapTest" FORMAT 'DNET' AUTHORIZATION OFF SECURE OFF 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.
Start Microsoft Visual Studio. Note that this example uses functions from the .NET Framework 2.0.
Create a new Windows application project using Visual C#.
An empty form appears.
From the Project menu, choose Add Web Reference.
In the URL field of the Add Web Reference page, enter the following URL: http://localhost:80/demo/SASoapTest_DNET.
Click Go.
You are presented with a list of the methods available for SASoapTest_DNET. You should see the EmployeeList method.
Click Add Reference to finish.
The Solution Explorer window shows the new web reference.
From the Visual Studio Toolbox, add a listbox and a button to the form as shown in the following diagram.
Rename the button text to Employee List.
Double-click Employee List and add the following code for the button click event.
int sqlCode; listBox1.Items.Clear(); 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.GetName(i); try { string value = dr.GetString(i); listBox1.Items.Add(columnName + "=" + value); } catch ( InvalidCastException ) { listBox1.Items.Add(columnName + "=(null)"); } } listBox1.Items.Add(""); } dr.Close(); |
Build and run the program.
The listbox will display the EmployeeList result set as column name=value pairs.
A try/catch block is included to handle NULL column values such as those found in the TerminationDate column of the Employees table.
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |