|
XML Web Services
Sample Service 2
In this section we will build a more interesting Web service that returns a ADO .NET
DataSet, containing the full set of records from a table. We will create our own database
table and access the data from the table with this Web service. To Start, open Microsoft
Access and create a new Database named Currency. Create a new table Table1 and add
three columns named, Country Code, Country Name and Currency. Enter some values in
the table and close it. Open Visual Studio .NET and select ASP.NET Web service from
the projects type template. Drag a OleDb connection from the Data tab in the toolbox
and using the properties window build a connection string that connects to the Currency
database which we created. Switch to the code view and start writing the following
code.
Imports System
Imports System.Web.Services
Imports System.Data.OleDb
'import this namespace as we are working with an OleDb source
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1 Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
#End Region
<WebMethod()> Public Function GetData() As DataSet
'WebMethod name is GetData,generate data set
Dim da as OleDbDataAdapter=new OleDbDataAdapter("Select * From Table1",_
OleDbConnection1)
'dataadapter
Dim ds As DataSet=new DataSet()
'declaring a new DataSet
da.Fill(ds, "Table1")
'filling dataadapter
Return ds
'returning dataset
End Function
End Class
|
Once you finish coding the Web service run the service by selecting Debug->Start
from the main menu or by pressing the F5 key on the keyboard. 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 the XML Web service method supported by a particular XML Web service.
Click the GetData 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.
|