简体   繁体   English

如何在没有Management Studio的情况下将数据库mdf文件附加到SQL Server Express?

[英]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.

相关问题 如何在Visual Studio中添加SQL Server数据库文件(.mdf)而不安装SQL Server Express Edition? - How to add SQL Server database file (.mdf) in Visual Studio without installing SQL Server Express Edition? 如何以编程方式将.MDF文件附加到Azure SQL Server C# - How to attach .MDF file to Azure SQL Server programmatically c# 如何在安装了SQL Express 2012但仍保留为SQL Express 2005的C#中附加mdf文件? - How to attach a mdf file in C# with SQL Express 2012 installed but keep as SQL Express 2005? 无法通过应用程序,SQL Server Management Studio或VS Server资源管理器访问SQL Server MDF文件 - Cannot access SQL Server MDF file through app, SQL Server Management Studio, or VS Server explorer 将LocalDB附加到Microsoft SQL Server Management Studio会破坏数据库 - Attach LocalDB to microsoft sql server management studio corrupts database 如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件 - How to backup a SQL Server 2014 Express Localdb (.mdf) file programmatically 如何使2个应用程序使用SQL Server Express访问相同的mdf文件 - How to get 2 applications accessing same mdf file with SQL Server Express 如何使用Winform Application部署和附加SQL Server Management Studio 2012数据库? - How to deploy and attach SQL Server Management Studio 2012 Database with Winform Application? 是否可以在没有SQL Server的情况下访问.mdf数据库? - Is it possible to access a .mdf database without SQL Server? 如何在没有任何服务器连接的情况下打开Database .mdf文件 - How to open the Database .mdf file without any Server connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM