Click here to Skip to main content
15,867,308 members
Articles / Database Development / SQL Server

LocalDB Manager

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
9 Aug 2013CPOL3 min read 69.5K   2.4K   33   11
This article explains working with SQL Server 2012 Express LocalDB.

Sample Image

Introduction

Microsoft SQL Server 2012 Express LocalDB is a special execution mode of SQL Server Express which includes a minimum set of files required to start SQL Server Database Engine. It allows using SQL Server without complex configuration tasks.

SQL Server Express LocalDB instances are managed by using the SqlLocalDB.exe utility. LocalDB can be used to work with SQL Server databases. System database files for a database are stored in the user's local AppData folder, for example:

C:\Users\Azim\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances

and user database files are stored in the user's documents folder, for example:

C:\Users\Azim

To demonstrate the use of LocalDB, I have created a Windows Forms Application using Microsoft Visual Studio Express 2012 for Windows Desktop, which allows a user to manage LocalDB instances.

Background

LocalDB supports two types of instances, Automatic and Named.

Automatic instances are automatically created and managed for the user and can be used by any application. One automatic instance of LocalDB exists for each version of LocalDB installed. There is no need to create the instance because it already exists. Automatic instances are named as the letter 'v' followed by the LocalDB release version in the format xx.x. For example, v11.0.

To connect to an automatic instance using SQL Server Management Studio, use (LocalDB)\v11.0 as the Server name as follows:

Image 2

Named instances are managed using the SqlLocalDB.exe application. The SqlLocalDB.exe program can be used to create, start, stop, destroy and get information about named instances.

The following command can be used to create a named instance called MYINSTANCE:

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" create MYINSTANCE

The newly created instance can be started by the following command:

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" start MYINSTANCE

Information about the instance can be found by the following command:

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" info MYINSTANCE

which produces the following output.

Image 3

The instance can be stopped by the following command:

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" stop MYINSTANCE

The instance can be removed by the following command:

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" delete MYINSTANCE

To connect to a named instance using SQL Server Management Studio, use the Instance Pipe Name as follows:

Image 4

Using the code

I have a developed a Windows Forms Application in C# using Microsoft Visual Studio Express 2012 for Windows Desktop to manage named instances of LocalDB. The application accepts an instance name from the user and allows the user to create, start, stop, delete and obtain information about the named instance. The output of the commands is displayed in a read-only multiline textbox.

Following is the user interface of the application:

Image 5

The application uses the System.Windows.Forms.OpenFileDialog class to locate the SqlLocalDB.exe file and the System.Diagnostics.Process class to manage the LocalDB instances.

Following is the full source code of the application:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace MyLocalDBManager
{
    public partial class Form1 : Form
    {
        FileDialog dialog;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            DoProcess("create");
            // Call the user-defined DoProcess() method to create a named instance.
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DoProcess("start");		// Start the named instance
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            DoProcess("stop");		// Stop the named instance
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DoProcess("delete");	// Remove the named instance
        }

        private void btnGetInfo_Click(object sender, EventArgs e)
        {
            DoProcess("info");		// Get Info about the named instance
        }

        private void DoProcess(string activity)
        {
            try
            {
                // Locate the SqlLocalDB.exe utility.
                // It is typically found in the
                // "C:\Program Files\Microsoft SQL Server\110\Tools\Binn" folder.
                dialog = new OpenFileDialog();
                dialog.Title = "Locate SqlLocalDB.exe";
                dialog.FileName = "SqlLocalDB.exe";
                dialog.Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*";
                dialog.FilterIndex = 1;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string command = "\"" + dialog.FileName + "\"";
                    ProcessStartInfo info = new ProcessStartInfo(command, " " + 
                      activity + " " + txtInstanceName.Text);	// Create a new ProcessStartInfo object
                    Process p = new Process();	// Create a new Process
                    p.StartInfo = info;		// Specify the StartInfo
                    p.StartInfo.UseShellExecute = false;	// Do not use the OS shell
                    p.StartInfo.RedirectStandardOutput = true;	// Allow writing output to the standard output
                    p.StartInfo.RedirectStandardError = true;	// Allow writing error to the standard error
                    p.Start();			// Start the process
                    p.WaitForExit();		// Wait for the process to exit
                    StreamReader reader = p.StandardOutput;	// Get Standard Output Stream
                    string output = reader.ReadToEnd();	// Read Standard Output
                    reader.Close();		// Close stream
                    if (output.Length == 0)	// If no output then display standard error
                    {
                        reader = p.StandardError;
                        string error = reader.ReadToEnd();
                        reader.Close();
                        txtResult.Text = error;	// Display error
                    }
                    else
                    {
                        txtResult.Text = output;// Display output
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

The above code uses a user-defined function called DoProcess which takes a parameter indicating the activity (create, start, stop, delete, or info) to be performed. This function performs the activity and captures its standard output or standard error and displays it in a multiline textbox.

Following is the typical output of the application:

Image 6

The instance pipe name can be copied from the multiline textbox and pasted in the Server name in the Connect to Server dialog in SQL Server Management Studio as follows:

Image 7

Points of Interest

LocalDB simplifies the task of working with Microsoft SQL Server 2012. Also since LocalDB runs under the user's security context, all database files used by a LocalDB instance can be accessed from the user's Windows Account.

I sincerely hope that my article will be helpful to someone out there who wants to work with Microsoft SQL Server 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
QuestionNice! Pin
snap-e-tom23-Jan-15 6:21
snap-e-tom23-Jan-15 6:21 
Questionthanks! Pin
trkchk14-Jan-15 4:27
trkchk14-Jan-15 4:27 
GeneralMy vote of 5! Pin
Agent__0071-Jul-14 20:33
professionalAgent__0071-Jul-14 20:33 
QuestionMy vote of 5! Pin
gersis7628-Jun-14 10:55
gersis7628-Jun-14 10:55 
AnswerRe: My vote of 5! Pin
Azim Zahir28-Jun-14 16:32
Azim Zahir28-Jun-14 16:32 
QuestionGreat job! Pin
dranko20-Jun-14 2:43
dranko20-Jun-14 2:43 
AnswerRe: Great job! Pin
Azim Zahir24-Jun-14 19:20
Azim Zahir24-Jun-14 19:20 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen26-May-14 22:22
professionalSunasara Imdadhusen26-May-14 22:22 
GeneralRe: My vote of 5 Pin
Azim Zahir6-Jun-14 17:32
Azim Zahir6-Jun-14 17:32 
GeneralGood Job! Pin
Rojan Gh.14-Apr-14 20:48
professionalRojan Gh.14-Apr-14 20:48 
GeneralRe: Good Job! Pin
Azim Zahir20-Apr-14 0:19
Azim Zahir20-Apr-14 0:19 
Thanks a lot. Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.