Wednesday, September 10, 2008

DB BackUp for per day schedule






PERFORMING DB BACKUP OPERATION USING TWO LINES QUERY


1. Create a SP to Back Up entireDatabase
TestingDB is the name of Database for which backup is formed and BackUp.bak is the backup file which will create when u will run query and D:\\ simply Disk Location.
BACKUP Database TestingDB
TO DISK = 'D:\BackUp.bak'
WITH Format

Go
2. Open vs and create a project (Console Application).
write code to call sp in Program.cs for example

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
namespace CMS_BACKUP
{
class Program
{
static void Main(string[] args)
{
CallBackUpDataBase();
}
private static void CallBackUpDataBase()
{
SqlConnection myConn=null;
try
{
SqlCommand cmd;
myConn = new SqlConnection("Data Source=;Initial Catalog=;User ID=sa;Password= ");
myConn.Open();
cmd = new SqlCommand("Back_Up_CMS", myConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{ }
finally
{
myConn.Close();
}
}
}
}
3. Build project to create exe. exe will create in Debug folder of Solution.
4. Now you can create a window service by using Schedule Task to schedule your exe.