Friday, March 14, 2008

Calling ASP.Net Webservice Through SQL Server

The main aim of this scenario is to call a webservice through sql server job.For example if you take any inventory application and want to mail stock details to some superiors of that company as per the schedule time.
Process:Here we have to Implement one sqlserver job which calls a EXE file that could be any .Net application.
That Exe file should contain a webservice reference.So that webservice will be invoked automatically whenever sql server job runs
XP_Cmdshell :It is a extended stored procedure available in SQl server used to execute particular EXE file from the specified path.
1)Create a Webservice:
Open visual studio IDE
File-> new->website->asp.net webservice application->select the specific language either C# or VB->specify the location to be saved
Implement a method which performs your require functionality as per the scheduled time .


Class WebServiceApplication
{
[WebMethod]
Public string helloworld()
{
Return “Function executed successfully”;
}
}
Build the application and check invoke whether its invoking properly or not
2)Create a one console application:
After opening the console applicationàGo to Project tab->add web reference ->copy and paste the webservice reference URLànd Click add reference
The reference of WebServiceApplication will be added to your console application.
Then create the object for WebServiceApplication class and call the respective methd as shown below
class consoleapplication
{
static void Main(string[] args)
{
WebServiceApplication objService = new WebServiceApplication();
strStatus = objService. helloworld();
}
}

Creating SQL Server Job and scheduling:
1)Open sql server enterprise manager
2)go to management ->select sql server agent->jobs
3)right click the job->new job->name the job and give the owener field as SA
4)then select STEPS from the window->click new
5)step window will appear->name the step as wish->type:transact sql script(T-Sql)->command = XP_cmdshell “console application exe file address” àclick ok
6)select schedule tab and schedule the duration for how many days once sql server has to run
7)say Ok







No comments: