Saturday, 21 March 2015

The Service on local computer started and then stopped ,Some services stop automatically if there are not in use by other services or programs

In my case, the windows service was working fine on Local, but while deploying on another machine. I created the service using SC commands which was done successfully, but on starting the service, I got this error.

Solution that worked for me:
1) On my local computer, cleared the data of Debug and Release folders in both bin and obj directories.

2) Then deployed, created and started the service successfully.

Run multiple SQL scripts in order using a batch file

Suppose, we have to run following sql files in the order:


1) CREATE_TABLES.sql
2) TABLE_INSERTS.sql
3) CREATE_INDEXES.sql
4) CREATE_PROCEDURES.sql

and suppose they are all present in the directory:
C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck


Now, create a script say CREATE_DB.sql 
SET NOCOUNT ON
GO

PRINT 'CREATING DATABASE'
IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'HELLODB')
DROP DATABASE HELLODB
GO
CREATE DATABASE HELLODB
GO

USE HELLODB
GO
:On Error exit

:r "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\CREATE_TABLES.sql"
:r "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\TABLE_INSERTS.sql"
:r "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\CREATE_INDEXES.sql"
:r "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\CREATE_PROCEDURES.sql"

PRINT 'DATABASE CREATE IS COMPLETE'
GO


If HELLODB already exists and you don't want to drop it, then remove the highlighted code above.

Now, create a new file, add the following code and save as demo.bat:

if HELLODB already exists:
SQLCMD -E -d HELLODB -i "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\create_db.sql"
PAUSE

if HELLODB does not exist:
SQLCMD -E -d master -i "C:\Users\Arjun\Documents\SQL Server Management Studio\BATCheck\create_db.sql"
PAUSE

Now, double click the demo.bat file and all the sql scripts will get executed.

Friday, 20 March 2015

How to create, start, stop and delete a Windows Service using SC commands

1) First go to the directory where the Windows Service executable (.exe file) is present.

2) Open Command Prompt.

3) To create service, enter the following command:

sc create DemoServiceName binpath= "C:\Users\Arjun\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe"

where,  DemoServiceName = name of the windows service


4) Click Start button, type services.msc and hit enter. You can see the service DemoServiceName  in the list.



5) To start service, enter the following command :

sc start DemoServiceName




6) To stopservice, enter the following command :

sc stop DemoServiceName



7) To delete service, enter the following command :

sc delete DemoServiceName