[英]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. 当前,我正在使用C#Windows应用程序,并且此应用程序正在使用SQL Server Express。
I have installed SQL Server and the Management Studio on my PC. 我已经在我的PC上安装了SQL Server和Management Studio。
I have installed SQL Server Express on the client's PC, but I don't want to install Management Studio as well. 我已经在客户端的PC上安装了SQL Server Express,但是我也不想安装Management Studio。
How do I attach my .mdf
and .ldf
files to SQL Server Express, without having SQL Server Management Studio installed? 如何在不安装SQL Server Management Studio的情况下将.mdf
和.ldf
文件附加到SQL Server Express?
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. 您应该在连接字符串中使用AttachDBFileName
参数,并将其指向您的MDF文件。 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|
对于ASP.NET应用程序, |DataDirectory|
refers to the App_Data
folder under your project. 指项目下的App_Data
文件夹。 I'm not sure what it refers to for a windows app, but I'm guessing you could figure it out pretty easily. 我不确定Windows应用程序指的是什么,但我想您可以很容易地弄清楚它。
Note that InitialCatalog
is not necessary when using AttachDBFileName
. 请注意,使用AttachDBFileName
时不需要InitialCatalog
。 InitialCatalog
is normally used to refer to a DB that the SQL Server instance already knows about. InitialCatalog
通常用于引用SQL Server实例已经知道的数据库。 AttachDBFileName
is used to instantiate a database from a given file. AttachDBFileName
用于从给定文件实例化数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.