简体   繁体   English

连接到其他计算机上的本地数据库

[英]Connect to local database on other computers

How can I use my LocalDB on other computers?如何在其他计算机上使用我的 LocalDB?

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 SQLServer because I am making a program for an offline computer.我不想使用 SQLServer,因为我正在为离线计算机制作程序。

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.如果您是按需附加 MDF 文件,那么无论如何这仅适用于安装在本地计算机上的 SQL Server 实例。

As suggested elsewhere, LocalDB is intended for development use only.正如其他地方所建议的,LocalDB 仅供开发使用。 Your users should be installing SQL Server Express and you can specify that in the connection string.您的用户应该安装 SQL Server Express,您可以在连接字符串中指定它。 If you're going to use a SQL Server data file, ie an MDF file, then you need a SQL Server instance to attach it to.如果您打算使用 SQL Server 数据文件,即 MDF 文件,那么您需要一个 SQL Server 实例来附加它。 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.如果您不想安装 SQL Server 实例,那么首先不要使用 SQL Server 数据文件。

LocalDb is sql server and is only intended for development purposes. LocalDb 是 sql server,仅用于开发目的。 It isn't something you would use for distributing an application.它不是您用来分发应用程序的东西。 SQL Express is an option for you, but may be overkill for what you need. SQL Express 是您的一个选择,但对于您的需要可能有点过分了。 You might look into SQLite as you might find it easier to embed and distribute with your application.您可能会研究 SQLite,因为您可能会发现它更容易嵌入和与您的应用程序一起分发。 Of course, the right answer depends on your actual requirements, etc.当然,正确答案取决于您的实际要求等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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