简体   繁体   中英

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. 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.

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. 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).

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