c# - 在连接字符串中设置到数据源的方式

标签 c# sql-server database ado.net

我的连接字符串是

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

但后来我把我的数据库复制到

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

并将其设置为连接字符串中的方式

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

但在那种情况下我有一个异常(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)

我需要什么正确的连接字符串?

最佳答案

如果这是一个“用户实例”对文件的访问,那么连接字符串更像是:

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

否则,使用初始目录提供在实例上注册的数据库名称。

关于c# - 在连接字符串中设置到数据源的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379277/

相关文章:

c# - 如何使用 Linq 查询 Mongo C# 2.2 驱动程序中的嵌套列表?

c# - Process.Start 参数问题 --- 转义 "="字符?

c# - 达到错误最大池大小?

sql-server - 如何从第二个表中选择所有ID在第一个表中存在的ID?

sql - 没有 WHERE 子句的 SELECT NOT NULL 值

java - 以 "type safe"方式做某事是什么意思?

java - 如何从数据库获取 spring.boot.admin.client.username/password 的值?

c# - 在 WinRT/UWP 中创建自定义虚拟化控件

java - Hibernate 中的 MSSQL 用户定义数据类型(缺少列异常)

database - 交错表术语