Friday, February 28, 2014

Generate All Database Backup From Sql Server Using Query

DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
DECLARE @filedate1 varchar(100)
SET @path = 'D:\BrijeshDatabase\'

SELECT @fileDate = CONVERT(varchar(50),GETDATE(),103)

SELECT @filedate1= REPLACE(@fileDate,'/','-')
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN db_cursor 
FETCH NEXT FROM db_cursor INTO @name 

WHILE @@FETCH_STATUS = 
BEGIN 
       SET @fileName = @path + @name + '_' + @filedate1 + '.BAK'
       BACKUP DATABASE @name TO DISK = @fileName

       FETCH NEXT FROM db_cursor INTO @name 
END 

CLOSE db_cursor 

DEALLOCATE db_cursor

Find Nth Highest Salary of Employee order by Desc

SELECT TOP 3 EmpSal
FROM (
SELECT DISTINCT TOP 3 EmpSal
FROM tblEmp1
ORDER BY EmpSal DESC) a
ORDER BY EmpSal

How to use OUTPUT option in an UPDATE statement

DECLARE @update_table TABLE (emp_no INT, project_no CHAR(20),old_job CHAR(20),new_job CHAR(20));

UPDATE works_on
SET job = NULL
OUTPUT DELETED.emp_no, DELETED.project_no,
DELETED.job, INSERTED.job INTO @update_table
WHERE job = 'Clerk';


SELECT * FROM @update_table

How the OUTPUT statement works with a DELETE statement

DECLARE @del_table TABLE (emp_no INT, emp_lname CHAR(20));

DELETE employee
OUTPUT DELETED.emp_no, DELETED.emp_lname INTO @del_table
WHERE emp_no > 15000;


SELECT * FROM @del_table

Wednesday, February 19, 2014

How to register WCF with IIS and ASP.NET

If we have installed .NET framework before installing IIS on our machine and we wanted to deploy a WCF service. Then we need to do some additional steps by registering Windows Communication Foundation with IIS and ASP.NET.

Firstly, register ASP.NET with IIS by executing following:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -i


Secondly, register WCF by executing following:
C:\Windows\Microsoft.NET\Framework\3.0\Windows Communication Foundation\ServiceModelReg -i

Find total week no and dates between two dates in asp.net c#

//============  Week No & Date Handling for Week No Dropdownbox ==================//
       
DateTime start = DateTime.Today;// Adjust to your start date

//------ Total week no of Current Year
int w = (DateTime.IsLeapYear(System.DateTime.Now.Year) ? 366 : 365) / 7;


//------ Week No of Current Date
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(start, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);


// Get date of first day of a current week.
DateTime dt = GetFirstDayOfWeek(start, ciCurr);
for (int x = weekNum; x <= w; x++)
{
dropWeekNo.Items.Add(string.Format("Week: {0} ({1} - {2})", weekNum, dt.ToString("dd/MM/yyyy").Replace('-', '/'), dt.AddDays(6).ToString("dd/MM/yyyy").Replace('-', '/')));
dt = dt.AddDays(7);
weekNum++;
}


 public DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo)
    {
        DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
        DateTime firstDayInWeek = dayInWeek.Date;
        while (firstDayInWeek.DayOfWeek != firstDay)
            firstDayInWeek = firstDayInWeek.AddDays(-1);

        return firstDayInWeek;

    }

Capture line number with exception message in asp.net C#

Sometime, i am writing a long code for implement for some logic in my program and inside my code, multiple try-catch block is exist, then i don't know proper error exception for capture message type. So i have capture all information for generated error.

Add below code in your function:


// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);

// Get the top stack frame
var frame = st.GetFrame(0);

// Get the line number from the stack frame

var line = frame.GetFileLineNumber();

Generate All Database Backup From Sql Server Using Query

DECLARE   @name   VARCHAR ( 50 )   -- database name DECLARE   @path   VARCHAR ( 256 )   -- path for backup files DECLARE   @fileName  ...