How to connect and access SQL Server 2012 Express LocalDB

This article describes how to connect and access SQL Server 2012 Express LocalDB.


In one instance the author describes how to use the utility to manage SqlLocalDB LocalDB, the paper will continue to introduce how to set up the database, as well as your application on the execution of individual LocalDB LocalDB and how to do integration.

For maximum benefit LocalDB have developed is easy to use, you do not need to manage the individual to perform various settings (such as authentication mode, protocol, service startup accounts, etc.), you can use [ instance name (localdb) ] as the server name to connect LocalDB instance using SQL Server 2012 Management Studio (including Express) or SQLCMD utility.

If you try to use SQL Server 2008 R2 (including) the previous management tools to connect to LocalDB instance, you will see an error message in the following figure.

Screenshot 00010

Screenshot 00011

因此,您可以到 Microsoft Download Center 下载 2012 版的 SQL Server Management Studio Express。以繁体中文版为例,32 位文件名称为【SQLManagementStudio_x86_CHT.exe】,64 位的文件名称则为【SQLManagementStudio_x64_CHT.exe】。安装完毕后如果你是使用 SSMS 2012 Express 连接 LocalDB 那就比较没有路径的问题,假设您使用的是 SQLCMD 公用程序,记得先切换到【C:Program FilesMicrosoft SQL Server110ToolsBinn】路径,在执行连接 LocalDB 的动作,以免因为默认路径的关系使用到 SQL Server 2012 之前版本的公用程序而无法连接 LocalDB 的执行个体。

Screenshot 00012

接下来为了示范方便,笔者使用 SSMS 2012 Express 来示范,介绍在 LocalDB 中建立数据库的注意事项。

当您的电脑中有安装 SSMS 2012 以前的版本时,在您第一次开启 SSMS 2012 会出现如下图的对话方块,告诉您 SSMS 2012 要将您在 SSMS 2008 的使用者设定数据导入到 2012,您可以依照您的需求来选择是否导入。

Screenshot 00013

接着于【连线到服务器】窗口中输入【(localdb)执行个体名称】,本文以 (localdb)LocalDB1 为示范。

Screenshot 00015

点选【对象总管 > 执行个体 > 数据库 > 新增数据库】来开启新增数据库窗口。

image

LocalDB 在建立数据库时,默认数据库文件路径是空白的。

Screenshot 00017

若您输入完数据库名称直接按确定,会看到如下图的错误消息。

Screenshot 00018

因此记得在输入完数据库名称后,指定数据库文件和事务记录档的路径,才可以顺利建立数据库。

Screenshot 00019

接着输入下列的 T-SQL 叙述来建立数据表及测试数据:

   1:  IF OBJECT_ID('tb1') IS NOT NULL 
   2:      DROP TABLE tb1
   3:  GO
   4:   
   5:  CREATE TABLE tb1
   6:  (
   7:      c1 int primary key,
   8:      c2 varchar(10)
   9:  )
  10:  GO
  11:   
  12:  INSERT INTO tb1 VALUES
  13:  (1,'user1'),
  14:  (2,'user2'),
  15:  (3,'user3')
  16:   
  17:  GO
  18:   
  19:  SELECT *
  20:  FROM tb1
  21:   
  22:  GO

执行结果如下:

Screenshot 00020

最后示范如何利用 Console 应用程序来存取 LocalDB 中的数据,程序如下,

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Data;
   6:  using System.Data.SqlClient;
   7:   
   8:  namespace Demo
   9:  {
  10:      class Program
  11:      {
  12:          static void Main(string[] args)
  13:          {
  14:              SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
  15:              scsb.DataSource = @"(localdb)LocalDB1";
  16:              scsb.InitialCatalog = "DB1";
  17:              scsb.IntegratedSecurity = true;
  18:              scsb.AttachDBFilename = @"C:MSSQLDB1.mdf";
  19:              using (SqlConnection con = new SqlConnection(scsb.ToString()))
  20:              {
  21:                  using (SqlCommand cmd = new SqlCommand("select count(*) from tb1", con))
  22:                  {
  23:                      if (con.State != ConnectionState.Open) con.Open();
  24:                      Console.WriteLine(cmd.ExecuteScalar().ToString());
  25:                  }
  26:              }
  27:              Console.ReadLine();
  28:          }
  29:      }
  30:  }

Column 18 wherein the program, using a database attribute AttachDBFilename additional actions, which is the option of choice.

[Reference Data]

  • SQL Server Management Studio Express

Original: Large columns  on how to connect and access SQL Server 2012 Express LocalDB


Guess you like

Origin www.cnblogs.com/chinatrump/p/11496675.html