In this article we would see how SQL Server 2005 has been powered to create Webservices with ease. Implementing real webserices in SQL Server 2005 is officially called HTTP Endpoints. Ok without fasting any further time lets start to understand HTTP Endpoint by getting our hands dirty.
Open SQL Server 2005
--Lets create a sample table populate it with few test data
Create Table WSDemo (Sno int, EmpName varchar(50), EmpMailId varchar(100))
Insert into WSDemo Values (1, 'Vadivel','firstname.lastname@example.org')
Insert into WSDemo Values (1, 'Velias','email@example.com')
--- Stored procedure which would return all employee name with their corresponding email address.
Create Procedure dbo.FetchWSDemo
Select EmpName, EmpMailId From WSDemo
--The actual WS work happens here
Create EndPoint FetchWSDemo
State = Started
Path = '/SQLWSDemo',
Authentication = (INTEGRATED),
Ports = (Clear),
Site = 'localhost')
Batches = Disabled,
Wsdl = DEFAULT,
Database = 'AdventureWorks',
Namespace = 'http://AdventureWorks/SQLWSDemo'
Points to note:
1) “State” can take one among the below three arguments:
Started— listening and responding
Disabled — neither listening nor responding
Stopped — listening, but returns errors to client requests.
2) Path – This clause specifies the URL on the server that clients will use to consume this Web service.
3) Authentication -- This clause specifies how clients will authenticate themselves to the SQL Server. For ex:
i) Integrated – most secure.
ii) Digest is not as secure as Integrated. You should use it only if Integrated authentication is not possible.
iii) Basic authentication is the least secure. It requires SSL as the Port value.
4) Ports – Clear
5) Site – The server name where the Web service is running
Open VS.NET 2005
Lets now create a client app to consume our webservice.
1. Create a new C# based windows application
2. Right click on the project name and choose Add >> Add web reference.
3. Type http://thinkingms.com/vadivel/ct.ashx?id=5e4247f6-13a0-4f44-a8cc-132e6e6fc816&url=http%3a%2f%2flocalhost%2fSQLWSDemo%3fwsdl and click on GO button. You will the webmethod come up in the list.
4. By default the name would be "localhost" change it to SQLWebServiceDemo
5. On the Form1 drag and drop a button and a Listview control.
6. Double click on the Button control and copy-paste the below code into it.
private void button1_Click(object sender, EventArgs e)
// Let us create a new instance of our web service
SQLWebServiceDemo.FetchWSDemo objWS = new SQLWebServiceDemo.FetchWSDemo();
/* We have to pass in credentials to authenticate into our service. Lets use the same credentials we have logged into our computer.*/
objWS.Credentials = System.Net.CredentialCache.DefaultCredentials;
DataSet objDS = new DataSet();
object objEmpData = objWS.FetchEmployeeData();
// iterate over the object array with a foreach statement,
// testing for the first object of type DataSet
foreach (object objArr in objEmpData)
if (objArr is DataSet) objDS = (DataSet)objArr;
listBox1.DataSource = objDS.Tables;
listBox1.DisplayMember = "EmpName";
listBox1.ValueMember = "EmpMailId";
Now we are all set to test our work. Just press F5 and click on the button to see the data being populated into the Listbox through our webservice/webmethod.