Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

How to back up SQL Server database

Database backup is routine activity you can take backup using SSMS and using C# code.

Back up MSSQL database using c# code:

You can use following c# code example to take backup of your SQL server database,

 C# Code:

   //references
    Microsoft.SqlServer.ConnectionInfo
    Microsoft.SqlServer.Management.Sdk.Sfc
    Microsoft.SqlServer.Smo
    Microsoft.SqlServer.SmoExtended
    Microsoft.SqlServer.SqlEnum
 
//Method,pass your databaseName, username, password, server name and destination path to save backup file
    public void BackupDatabase(string databaseName, string userName, string password, string serverName, string destinationPath)
    {            //Define a Backup object variable. 
      Backup sqlBackup = new Backup();
    //Specify the type of backup, the description, the name, and the database to be backed up.
 
        sqlBackup.Action = BackupActionType.Database;
 
        sqlBackup.BackupSetDescription = "BackUp of:" + databaseName + "on" + DateTime.Now.ToShortDateString();
 
        sqlBackup.BackupSetName = "FullBackUp";
 
        sqlBackup.Database = databaseName;
        //Declare a BackupDeviceItem
 
        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath + "FullBackUp.bak", DeviceType.File);
 
        //Define Server connection
 
        ServerConnection connection = new ServerConnection(serverName, userName, password);            //To Avoid TimeOut Exception
 
        Server sqlServer = new Server(connection);
        sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
        Database db = sqlServer.Databases[databaseName]; (Reference Database As microsoft.sqlserver.management.smo.database,not as System.entity.database )
 
sqlBackup.Initialize = true;
 
        sqlBackup.Checksum = true;
 
        sqlBackup.ContinueAfterError = true;
        //Add the device to the Backup object.
 
        sqlBackup.Devices.Add(deviceItem);
 
        //Set the Incremental property to False to specify that this is a full database backup.
        sqlBackup.Incremental = false;
        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
 
        //Specify that the log must be truncated after the backup is complete.       
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
        sqlBackup.FormatMedia = false;
 
        //Run SqlBackup to perform the full database backup on the instance of SQL Server.
        sqlBackup.SqlBackup(sqlServer);
 
        //Remove the backup device from the Backup object.          
        sqlBackup.Devices.Remove(deviceItem);
    }
    }


This code will take backup of your all databases in SQL server & As you can see in the above method created using C#, you need to pass all the details of your database like Server Name, User Name, Password, Destination path etc.

There are more options to take back-up of sql server database and is the original source of the above code.

let in TypeScript

TypeScript (and ES6) introduces the let keyword to allow you to define variables with true block scope. That is if you use let instead of var you get a true unique element disconnected from what you might have defined outside the scope.

Following example code example demonstrates use of “let” which is truly block scoped.

Function getUserNameWithLet() returns username "nmk" which is block scoped.

Typescript Code:

function getUserNameWithVar()
{
    var userName = "ken";
    if (true) {
       var userName = "kendre";
    }
    return userName;
}
 
function getUserNameWithLet() {
    let userName = "nmk";
    if (true) {
        let userName = "kendre";
    }
    return userName;
}
 
window.onload = () => {
    var elNameWithVar = document.getElementById('lblFirstNameWithVar');
    var elNameWithLet = document.getElementById('lblFirstNameWithVar');
    elNameWithVar.innerHTML =getUserNameWithVar();
     elNameWithLet.innerHTML =getUserNameWithLet();
};

HTML code:

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script src="LetExample.js"></script>
</head>
<body>
    <h1>TypeScript HTML App</h1>
 
<label id="lblFirstNameWithVar"></label>
<label id="lblFirstNameWithLet"></label>
</body>

Enums in TypeScript

Enums 

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type(reference wikipedia.org).

Enum allows us to create set of named constants, common use is we can create distinct cases. TypeScript provides both numeric and string-based enums.

We can create three types of enum  in typescript following examples will explain in details , how to create enums.


1. Numeric enums

We will start with numeric enums, an enum can be defined using the enum keyword.

Enum Visibility
    {
        None=1,
        Hidden,
        Visible
    };

Above, we have a numeric enum where Up is initialized with 1. All the following members are auto-incremented from that point on. In other words, Visibility.None has the value 1, Hidden has 2, Visible has 3.

enum Visibility
{
    None,
    Hidden,
    Visible
};

Here, None would have the value 0Hidden would have 1, etc. This auto-incrementing behavior is useful for cases where we might not care about the member values themselves, but do care that each value is distinct from other values in the same enum.

How to use it:

Using an enum is simple: just access any member as a property off the enum itself, and declare types using the name of the enum, check following example where we used enum to set text display mode.

Example:

function
setVisibiity(recipient: string, message: Visibility): void {
    // ...
}

setVisibiity("Aditya Balaji Kendre", Visibility.Visible)


2. String enums

In a string enum, each member must be constant-initialized with a string literal, or with another string enum member.

How to use it:

function
SetUserFevColor(recipient: string, userColorChoice: Colors): void {
    // ...some typescript custom code
}
 
SetUserFevColor("Ajay Balaji Kendre", Colors.Green);
 


3. Heterogeneous enums

Technically enums can be mixed with string and numeric members,but its recommended to avoid Heterogeneous enums except must require case.

Example:

enum
UpdatedData  {
    No = 0,
    Yes="YES"
}