The following tutorial demonstrates how to access web services from Microsoft .NET using Visual C#.
Copy the SQL Anywhere sample database from samples-dir to another location, such as c:\webserver\demo.db.
At a command prompt, execute the following statement to start a personal web server. The -xs http(port=80)
option tells the database server to accept HTTP requests. If you already have a web server running on port 80, use another port number such as 8080 for this tutorial.
dbeng10 -xs http(port=80) c:\webserver\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 C#. Note that this example uses functions from the .NET Framework 2.0.
Create a new Windows Application project.
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.
Add a ListBox and a Button to the form as shown in the following diagram.
Rename the button text to Employee List.
Double-click the Employee List button 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); string value = dr.GetString(i); listBox1.Items.Add(columnName+"="+value); } listBox1.Items.Add(""); } dr.Close();
Build and run the program.
The listbox will display the EmployeeList result set as column name=value pairs.