简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM