| |
| XML Web Services |
| Data access WebMethod |
In this walkthrough, you will create a
WebMethod that returns a complex type; a DataSet representing
rows in the Orders database table.
- Open the myFirstWebService.asmx file in Code view.
- From the Code Builders tab in the Toolbox, drag a
SELECT Data Method Code Builder onto the XML Web Service page.
- You will see the Connect to Database dialog box appear.
- Type localhost in the Server box (default).
- Click the Database drop-down. ASP.NET Web Matrix will connect
to the data source and populate the Database drop-down with
database names. Select the Orders database that we created in an
earlier section walkthrough.
- Click OK.
- You will see step one of the Code Builder Wizard appear; it is
called the Construct SELECT Query dialog box.
- Select Orders in the Tables list view.
- Select the check box next to the * item in the Columns
list view.
- Click Next.
- You will see step two of the Code Builder Wizard appear; it is
called the Query Preview dialog box.
- Click Test Query.
- You will see the Orders rows appear in the Query Preview
dialog box results grid.
- Click Next.
- You will see step three of the Code Builder Wizard appear; it
is called the Enter A Caption dialog box. Type GetOrders
into the method name box.
- Select the DataSet radio button (default).
- Click Finish.
- The Wizard will close and ASP.NET Web Matrix will place a
function called GetOrders in Code view. The function accepts
no input parameters and returns a DataSet as an output parameter.
The DataSet is populated with all of the orders in the Orders
table.
- Add a WebMethod attribute to the GetOrders function. The
WebMethod accepts no input parameters and returns a DataSet
as an output parameter. The DataSet is populated with all of the
rows in the Orders table:
<WebMethod> Function GetOrders() As System.Data.DataSet
Dim connectionString As String = _
"server='localhost'; trusted_connection=true; Database='Orders'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = _
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [Orders].* FROM [Orders]"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = _
New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = _
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = _
New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
- Save and run the XML Web Service page.
- Your XML Web Service page will automatically appear in a new instance
of your Web browser.
- You will see the XML Web service's HTML description file. The XML Web
service's HTML description page shows you all the XML Web service methods
supported by a particular XML Web service.
- Click the GetOrders link at the top of the HTML description
file.
- Click Invoke.
- The Web service will return an XML response that shows all of the rows
in the Orders table in a new instance of your Web browser.
- Close the Web browser instance.
Next Step >> |
|