Archive

Archive for February, 2010

How to debug windows service

February 24, 2010 Leave a comment

Debugging a Windows service under Visual Studio .NET is not straight forward alternative is to create a console application project and then after successful functionality with debugging use the same code in windows service.

Solution to this is that when you create new windows service project  Main() function is available in  Program.cs class.

Do following modification in Program.cs and debug your windows service normally.

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
//comment these lines while debug
//un-comment when you want to install windows service
//ServicesToRun = new ServiceBase[]
//{
//    new Service1()
//};
//ServiceBase.Run(ServicesToRun);

//comment these lines when you want to install windows service and
//un-comment when you want to debug
Service1 service = new Service1();
service.MethodCalled();//new method added wchich will be called
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

}
}

Now do following changes in Service1.cs

public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{

//comment these lines when you want to install windows service and
//un-comment when you want to debug
// MethodCalled();
}

protected override void OnStop()
{
}

public void MethodCalled()
{

//Debug will come here after adding break point write your windows service code in this method
}

Using this when you start debugging in Visual Studio debugger will come in method MethodCalled()

Found this solution at http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx by By Lee Humphries

Advertisements

#temp table or a @table variable

February 21, 2010 Leave a comment

In a stored procedure, you often have a need for storing a set of data within the procedure, without necessarily needing that data to persist beyond the scope of the procedure.

1. Local Temporary Tables(# table)

CREATE TABLE #employee
(
empid INT,
empname VARCHAR(100)
)

  • A temporary table is created and populated on disk, in the system database tempdb — with a session-specific identifier packed onto the name, to differentiate between similarly-named #temp tables created from other sessions.
  • The data in this #temp table is visible only to the current scope usually a stored procedure, or a set of nested stored procedures.
  • The table gets cleared up automatically when the current procedure goes out of scope but try to delete before exiting stored procedure.
  • Local temp tables name is limited to 116 characters.

Use DROP TABLE statment like following.

DROP TABLE #people

2.Table Variables(@ table)

DECLARE @employee TABLE
(
empid INT,
empname VARCHAR(100)
)

  • A table variable is created in memory, and so performs slightly better than #temp tables.
  • Table variables are automatically cleared when the procedure or function goes out of scope, so you don’t have to remember to drop or clear the data.
  • You cannot truncate a table variable.
  • Table variables cannot be altered after they have been declared.
  • Unlike a #temp table, you cannot drop a table variable when it is no longer necessary—you just need to let it go out of scope.