简体   繁体   English

设置连接字符串中数据源的方式

[英]set the way to the data source in connection string

My conection string was 我的连接线是

string connStr = @"Data Source=(local)\SQLEXPRESS
                        Initial Catalog=University11;
                        Integrated Security=True";

But then I copied my database to 但是后来我将数据库复制到

C:\Users\Чак\Desktop\ботанизм\ООП\coursework.start\CourseWorkFinal\CourseWorkFinal\

And set it as the way in connection string 并将其设置为连接字符串中的方式

  string connStr = @"Data Source=C:\Users\Чак\Desktop\ботанизм\ООП\coursework.start\CourseWorkFinal\CourseWorkFinal\;
                        Initial Catalog=University11;
                        Integrated Security=True";

But in that case I had an exception 但在那种情况下,我有一个例外

A network-related or instance-specific error occurred while establishing a connection 
to SQL Server. The server was not found or was not accessible. Verify that the instance 
name is correct and that SQL Server is configured to allow remote connections. 
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance 
Specified)

What correct connection string do I need? 我需要什么正确的连接字符串?

If that is intended to be a "user instance" access to a file, then the connection string is more like: 如果打算将其作为对文件的“用户实例”访问,则连接字符串更像:

Data Source=.\SQLEXPRESS;AttachDBFilename=YourPath.mdf;Integrated Security=True;

Otherwise, use the Initial Catalog to supply a database name registered on the instance. 否则,请使用“初始目录”来提供在实例上注册的数据库名称。

The connection string ( Data Source=(local)\\SQLEXPRESS ...) is intended for hiding the physical location of the database files when you decide to move files. 连接字符串( Data Source=(local)\\SQLEXPRESS ...)用于在您决定移动文件时隐藏数据库文件的物理位置。 No matter where your files are, the programs that use your database should not care, because logically it's the same database. 无论您的文件位于何处,使用数据库的程序都不在乎,因为从逻辑上讲它是同一数据库。 When you move your DB files, you need to re-point your SQL Express database to the new location, and keep the connection string intact. 移动数据库文件时,需要将SQL Express数据库重新指向新位置,并保持连接字符串不变。

I think you shouldn't change your connention string, you can't access SQL SERVER database directly you must always pass through the SQL SERVER DBMS. 我认为您不应该更改您的连接字符串,您不能直接访问SQL SERVER数据库,而必须始终通过SQL SERVER DBMS。 The only thing you can do is import/export your data or manage your SQL SERVER to read your database from the new destination folder (but this must be done within SQL SERVER not modifying the connection string). 您唯一可以做的就是导入/导出数据或管理SQL SERVER以从新的目标文件夹中读取数据库(但这必须在SQL SERVER中完成,而不修改连接字符串)。

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

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