Search code examples
c#sqldatabase-connectionlocal-database

Connect to local database on other computers


How can I use my LocalDB on other computers?

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v.11.0;AttachDbFilename=[DataDirectory]\Database1.mdf;Integrated Security=True");

I tried this but it doesn't work because I get this error:

Local Database Runtime error occurred. Cannot create an automatic instance

Does someone know how to fix this, or what I can do instead?

I don't want to use SQL Server because I am making a program for an offline computer.


Solution

  • The fact that your app is for an offline computer is irrelevant. If you're attaching an MDF file on demand then that will only work with a SQL Server instance installed on the local machine anyway.

    As suggested elsewhere, LocalDB is intended for development use only. Your users should be installing SQL Server Express and you can specify that in the connection string. If you're going to use a SQL Server data file, i.e. an MDF file, then you need a SQL Server instance to attach it to. If you don't want to have to have a SQL Server instance installed then don't use a SQL Server data file in the first place.