Data Retrieval Made Easy
Retrieve Data from a Database Using a Web Service
October 30, 2009
asp:Feature
Data Retrieval Made Easy
Retrieve Data from a Database Using a Web Service
By Ricardo D. Sanchez
Web services are a great solution when you need to exposedata to different applications using XML to transfer data.
Although Web services are not .NET-specific, by usingVisual Studio 2005, the task of creating Web services and applications toconsume them is very easy. In this article I ll show you how to create a Webservice that will connect to a SQL database, and an application that will connectto the Web service to retrieve the data.
For this example we re going to connect to the Northwindsample database using one of its existing stored procedures.
Creating the Web Service
Open Microsoft Visual Studio (any 2005 version) and createa New Web Site ASP.NET Web Service. Make sure the name of your newly created Webservice project is WS_Northwind (to maintain consistency with this article).The code added by Visual Studio is a complete, albeit very simple, Web service.It even contains a WebMethod for the famous Hello World .
Configuring the Connection to the Database
Let s create the connection to our Northwind databaseusing Visual Studio s new ASP.NET configuration tool. Click on Website in thetop menu, then click on ASP.NET Configuration. A Web page will open in abrowser window (see Figure 1).
Figure1: Web Site AdministrationTool.
Next, click on Application Configuration. UnderApplication Settings, click on Create Application Settings. Type the followingto create a connection string, then click Save and OK (see Figure 2):
Name: NorthwindConnString
Value: server=localhost;database=Northwind;uid=user;password=password; (Replace thevalues for user and password with a user name and password for your server).
Figure 2: Web Site AdministrationTool Application setting.
Creating Functions to Get the Data from the Northwind Database
We now need to create the functions that will call theNorthwind stored procedure using the connection string we ve just created tothen create WebMethods that will call the functions that return a data set.
With the service.vb page open (this is the page that wascreated automatically by Visual Studio when you created the WebServiceproject), type the following on the very top:
Imports System.Data
Imports System.Data.SqlClient
Then, under the Hello World WebMethod function, type thefollowing function (which will retrieve the data from the stored procedure):
'Function to get data from database
Public FunctionGetSalesByYear(ByVal Beginning_Date As DateTime, ByVal Ending_Date As DateTime)As DataSet
' Create Instance ofConnection and Command Object
Dim objSQLConn As NewSqlConnection(ConfigurationManager.AppSettings("NorthwindConnString"))
Dim objSQLAdapter AsNew SqlDataAdapter("Sales by Year", objSQLConn)
Dim dsTemp As NewDataSet
' Mark the Command as aSPROC
objSQLAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
objSQLAdapter.SelectCommand.Parameters.Add(NewSqlParameter("@Beginning_Date", SqlDbType.DateTime))
objSQLAdapter.SelectCommand.Parameters("@Beginning_Date").Value= Beginning_Date
objSQLAdapter.SelectCommand.Parameters.Add(NewSqlParameter("@Ending_Date", SqlDbType.DateTime))
objSQLAdapter.SelectCommand.Parameters("@Ending_Date").Value =Ending_Date
Try
' Execute the commandby filling the dataset
objSQLConn.Open()
objSQLAdapter.Fill(dsTemp)