NoSQL Zone is brought to you in partnership with:

Adam is an Evangelist for Windows Azure, working for Microsoft. By day, you can likely find him somewhere in the midwest, driving to yet another whiteboarding/deep-thinking session, ready to figure out how the cloud can save your family from certain doom, and make you rich and successful in the process. Before he started evangelizing, Adam was a Senior Developer Lead for Microsoft in Redmond, working on Office 365, BPOS, and Office Live. He misses Redmond, and the excitement of the mother ship, but the call of bitter cold and lots of snow in Chicago was too much for him, and he had to return. Lucky for you! When he's not evangelizing, he likes to spend time with his wife and kids, telling them how the cloud will benefit them as well. Adam is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

LocalDB Using SQL Server Management Studio 2012 (SSMS)

08.25.2012
| 6787 views |
  • submit to reddit

Ok, in the previous post we determined how we could attach to LocalDB using SSMS 2012. Next stop, creating a database. Turns out that it's potentially harder than it seems at first.

Now that I've gotten connected, I went to try to create a database. Right click on the connection, create new database. Fill in the name, press OK.

Boom. Errors like the following:

TITLE: Microsoft SQL Server Management Studio
------------------------------

Create failed for Database 'test'.  (Microsoft.SqlServer.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=11.0.2100.60+((SQL11_RTM).120210-1846+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Create+Database&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------

A file activation error occurred. The physical file name 'test.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors. (Microsoft SQL Server, Error: 5105)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=11.00.2100&EvtSrc=MSSQLServer&EvtID=5105&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------

... and digging into the details, looks like the following:

===================================

Create failed for Database 'test'.  (Microsoft.SqlServer.Smo)

------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=11.0.2100.60+((SQL11_RTM).120210-1846+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Create+Database&LinkId=20476

------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()
   at Microsoft.SqlServer.Management.SqlManagerUI.CreateDatabaseData.DatabasePrototype.ApplyChanges(Control marshallingControl)
   at Microsoft.SqlServer.Management.SqlManagerUI.CreateDatabase.DoPreProcessExecution(RunType runType, ExecutionMode& executionResult)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.DoPreProcessExecutionAndRunViews(RunType runType)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.ExecuteForSql(PreProcessExecutionInfo executionInfo, ExecutionMode& executionResult)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.Microsoft.SqlServer.Management.SqlMgmt.IExecutionAwareSqlControlCollection.PreProcessExecution(PreProcessExecutionInfo executionInfo, ExecutionMode& executionResult)
   at Microsoft.SqlServer.Management.SqlMgmt.ViewSwitcherControlsManager.RunNow(RunType runType, Object sender)

===================================

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
   at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
   at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ExecuteNonQuery(StringCollection queries, Boolean includeDbContext)
   at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImplFinish(StringCollection createQuery, ScriptingPreferences sp)
   at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()

===================================

A file activation error occurred. The physical file name 'test.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors. (.Net SqlClient Data Provider)

------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=11.00.2100&EvtSrc=MSSQLServer&EvtID=5105&LinkId=20476

------------------------------
Server Name: (localdb)\v11.0
Error Number: 5105
Severity: 16
State: 2
Line Number: 1


------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes exe

So what's going on here? I open up a query window, type the following and run it:

create database [test]
go

That works fine? What gives?

Rooting around the file system a bit gives the answer. When creating a database via DDL script, like this, the default output directory is "c:\users\Adam" (yours will be your name, of course), and the database files end up in that directory. Looking at my drive, I now see test.mdf and test_log.mdf in that directory. However, looking at where SSMS wants to create them reveals the problem. In the database files section of the General page in the New Database dialog, scroll over to the right and have a look at the Path location. It's set to <default path>. Now click on the dot-dot-dot (ellipsis) and take a look at what that default path is...

C:\Users\Adam\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0

That's not gonna work, and besides, I'd never find the MDF file again if I put it in there.

So, change that path to "c:\users\Adam" in the Create Database dialog fixes the problem, or just use the "create database" DDL instead.

Published at DZone with permission of Adam Hoffman, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: