How to manually create a mdf file for localdb to use?
Just use CREATE DATABASE statement
SqlConnection connection = new SqlConnection(@"server=(localdb)\v11.0");
using (connection)
{
connection.Open();
string sql = string.Format(@"
CREATE DATABASE
[Test]
ON PRIMARY (
NAME=Test_data,
FILENAME = '{0}\Test_data.mdf'
)
LOG ON (
NAME=Test_log,
FILENAME = '{0}\Test_log.ldf'
)",
@"C:\Users\George"
);
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
}
Not sure what you mean by "manually". I'll add an option using Visual Studio 2013 and LocalDb:
Open Server Explorer, right-click on Data Connections, select Create New SQL Server Database. For "Server Name" use "(LocalDB)\v11.0".
There is another option, as described here but it requires installation of SQL Server Data Tools. A version of the instructions for Visual Studio 2012 is also available.
Since you also mention SQL Server Management Studio, you can simply connect to the LocalDb instance and right-click on Databases, then Create, the standard way. It is more-or-less a regular SQL Server instance and all standard operations will function as usual.
Creating the database can also, obviously, be done from the application code as well, but that requires setting up appropriate database permissions. Depending on your environment that may or may not be a good idea.
I know, old question, but I find the following way still relevant and quick. Here are the full steps, but actually only the last part is really relevant:
Pre-requisites:
- MS Sql LocalDb engine
- MS Sql Server Management Studio
Steps:
- Open command prompt
- Run
SqlLocalDb info
to list currently installed LocalDb instances. There should be at leastv11.0
for Sql Server 2012/Visual Studio 2012 orMSSQLLocalDB
for Sql Server 2014/Visual Studio 2015 - Open Sql Server Management Studio, or show Connect dialog if already running
- Use Server name
(localdb)\v11.0
or(localdb)\MSSQLLocalDB
, whichever you're interested into. Select Windows Authentication - Create a new query
Paste the following template, adapting your path and names as needed:
CREATE DATABASE __YourDbName__ ON ( NAME='__YourDbName__', FILENAME='YourDrive:\Your\path\to\data\files\__YourDbName__.mdf')
Run query
- Refresh Object Explorer list of Databases
In Object Explorer you should now see the newly created DB, while in Windows Explorer you should now see the newly created .mdf
and .ldf
files at specified path.
HTH