Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to create SQL server 2016 localdb using C# code?

How to create SQL server 2016 localdb using C# code?

Scheduled Pinned Locked Moved Database
databasesql-serverquestioncsharpsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    w14243
    wrote on last edited by
    #1

    SQL Server 2016 LocalDB installed, and instance 'FFw1a' is created and running: C:\Program Files\Microsoft SQL Server\130\Tools\Binn>sqllocaldb i Ffw1a Name: FFw1a Version: 13.0.2151.0 Share name: Owner: MinKyung\icey.dong Auto Create: No Status: Running Start Time: 2017/3/3 16:48:38 Pipe name: np:\\.\pipe\LOCALDB#E10BE0E6\tsql\query I want to create a local db file by below C# code: private void CreateDatabase() { System.Data.SqlClient.SqlConnection tmpConn; string sqlCreateDBQuery; tmpConn = new SqlConnection(); tmpConn.ConnectionString = @"Data Source=(LocalDB)\FFw1a;AttachDbFilename='D:\y\2\TestDB_Data.mdf'; Integrated Security=True;Connect Timeout=30;Encrypt=False"; sqlCreateDBQuery = "CREATE DATABASE TestDB ON PRIMARY "+ @"(NAME = TestDB_Data, FILENAME = 'D:\y\2\TestDB_Data.mdf', SIZE = 2MB, FILEGROWTH = 10%) "+ @"LOG ON (NAME = TestDB_Log, FILENAME = 'D:\y\2\TestDB_Log.ldf', SIZE = 1MB, FILEGROWTH = 10%)"; SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn); try { tmpConn.Open(); myCommand.ExecuteNonQuery(); MessageBox.Show("Database has been created successfully!", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (System.Exception ex) { MessageBox.Show(ex.ToString(), "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { tmpConn.Close(); } return; } But it cause exception on 'tmpConn.Open()', and the message is: An attempt to attach an auto-named database for file D:\y\2\TestDB_Data.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. How can I change the connection string and sqlCreateDBQuery to create a database file successfully?

    C 1 Reply Last reply
    0
    • W w14243

      SQL Server 2016 LocalDB installed, and instance 'FFw1a' is created and running: C:\Program Files\Microsoft SQL Server\130\Tools\Binn>sqllocaldb i Ffw1a Name: FFw1a Version: 13.0.2151.0 Share name: Owner: MinKyung\icey.dong Auto Create: No Status: Running Start Time: 2017/3/3 16:48:38 Pipe name: np:\\.\pipe\LOCALDB#E10BE0E6\tsql\query I want to create a local db file by below C# code: private void CreateDatabase() { System.Data.SqlClient.SqlConnection tmpConn; string sqlCreateDBQuery; tmpConn = new SqlConnection(); tmpConn.ConnectionString = @"Data Source=(LocalDB)\FFw1a;AttachDbFilename='D:\y\2\TestDB_Data.mdf'; Integrated Security=True;Connect Timeout=30;Encrypt=False"; sqlCreateDBQuery = "CREATE DATABASE TestDB ON PRIMARY "+ @"(NAME = TestDB_Data, FILENAME = 'D:\y\2\TestDB_Data.mdf', SIZE = 2MB, FILEGROWTH = 10%) "+ @"LOG ON (NAME = TestDB_Log, FILENAME = 'D:\y\2\TestDB_Log.ldf', SIZE = 1MB, FILEGROWTH = 10%)"; SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn); try { tmpConn.Open(); myCommand.ExecuteNonQuery(); MessageBox.Show("Database has been created successfully!", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (System.Exception ex) { MessageBox.Show(ex.ToString(), "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { tmpConn.Close(); } return; } But it cause exception on 'tmpConn.Open()', and the message is: An attempt to attach an auto-named database for file D:\y\2\TestDB_Data.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. How can I change the connection string and sqlCreateDBQuery to create a database file successfully?

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      Change the file names in the CREATE DATABASE statement so they use different file names to the one in your original connection string. You use AttachDbFilename='D:\y\2\TestDB_Data.mdf' in the connection string, then try to create a database file of the same name.

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      W 1 Reply Last reply
      0
      • C Chris Quinn

        Change the file names in the CREATE DATABASE statement so they use different file names to the one in your original connection string. You use AttachDbFilename='D:\y\2\TestDB_Data.mdf' in the connection string, then try to create a database file of the same name.

        ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

        W Offline
        W Offline
        w14243
        wrote on last edited by
        #3

        Tried, but also failed. The exception is caused on 'tmpConn.Open()', not create databse anymore.

        K 1 Reply Last reply
        0
        • W w14243

          Tried, but also failed. The exception is caused on 'tmpConn.Open()', not create databse anymore.

          K Offline
          K Offline
          kosmas kafataridis
          wrote on last edited by
          #4

          tmpConn.ConnectionString = @"Data Source=(LocalDB)\FFw1a;AttachDbFilename='D:\y\2\TestDB_Data.mdf'; Integrated Security=True;Connect Timeout=30;Encrypt=False";

          remove this

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups