Yash
Yash

Reputation: 181

How to attach database mdf file to SQL Server Express without Management Studio?

Currently I am working on a C# Windows application, and this application is using SQL Server Express.

I have installed SQL Server and the Management Studio on my PC.

I have installed SQL Server Express on the client's PC, but I don't want to install Management Studio as well.

How do I attach my .mdf and .ldf files to SQL Server Express, without having SQL Server Management Studio installed?

This is my connection string

Data Source=.\\SQLEXPRESS;Initial Catalog=dbname;Integrated Security=False;User Id=sa;Password=password;Connect Timeout=0

Upvotes: 2

Views: 2359

Answers (1)

Jimmy
Jimmy

Reputation: 28376

You should use the AttachDBFileName parameter in your connection string and point it to your MDF file. There is a special value you can put into this to refer to a local data directory so that you don't need to use a hardcoded path:

AttachDbFileName=|DataDirectory|\MyDatabase.mdf

For ASP.NET applications, the |DataDirectory| refers to the App_Data folder under your project. I'm not sure what it refers to for a windows app, but I'm guessing you could figure it out pretty easily.

Note that InitialCatalog is not necessary when using AttachDBFileName. InitialCatalog is normally used to refer to a DB that the SQL Server instance already knows about. AttachDBFileName is used to instantiate a database from a given file.

Upvotes: 1

Related Questions