Restoring a SQL Server database backup file (.bak)

Introduction On regular basis we need to restore the database, either will only schema of the database, or many times both data as well as schema.  Most of the applications (either Web application e.g. ASP.NET based apps, or Windows applications e.g. WPF based apps) requires 'reference data' such as drop-down data, types of users, etc. So what is the most optimum, reliable way of moving database either on separate database server, or on the same database server but with different database name? There are several ways to achieve this objective but my personal preference is to first create the database backup file i.e. 'my_backup.bak' using Microsoft SQL Server Management Studio , and then restoring that backup file to create new database.  This article focuses on restoring the backup file to create a new database. How to create the backup file will be covered in another article.   This article addresses below points: 1.   How to check the name and path of the files

Using SqlCE - Cheatsheet

Introduction

This post will provide quick cheatsheet to start using SqlCE in your Microsoft dotnet based projects.

Microsoft SQL Server Compact (SQL CE)

►  First add reference to the System.Data.SqlServerCe by installing the Nuget package - "System.Data.SqlServerCe_unofficial".
    
►  The connection string
Data Source=recordvault.sdf;password=yourpwd
       
►  Create the database file first
using (SqlCeEngine engine = new SqlCeEngine(_connString)) {
    engine.CreateDatabase();
}

    
►  Open the Connection
_conn = new SqlCeConnection(_connString);
    
►  There are mainly two types of command's methods can be used based the query type.
    
►  First is where you do not expect any output
await _conn.OpenAsync();
SqlCeCommand cmd = new SqlCeCommand {
    Connection = _conn,
    CommandText = "create table Test (Id int, Name nvarchar(255))"
};
await cmd.ExecuteNonQueryAsync();

    
►  And second where you expect data
await _conn.OpenAsync();
SqlCeCommand cmd = new SqlCeCommand {
    Connection = _conn,
    CommandText = "select Id, Name from Test"
};
DbDataReader reader = await cmd.ExecuteReaderAsync();
if (reader.Read()) {
    success = reader["Name"].ToString();
}

Summary

We provided standard Nuget package name, namespace, connection string format, and finally two types of commands i.e. first is to execute the query where we are not expecting any output, and second command is to execute and get result-set back from the database.

 

Your feedback

Your feedback is of paramount importance to us, provide your valuable inputs; what else you would like to read about, or if you would like the author to elaborate on any specific topic from this article, or any other article on this blog.

Disclaimer

The material presented in this blog is to help educate the developers, and the author(s) & owner(s) of this blog are not responsible of any damage arising out of use of the code snippets or any other contents of this blog.

Comments