gpt4 book ai didi

c# - 错误 - 尝试连接到 sql server 数据库时出现 "The server was not found or was not accessible"

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:29 24 4
gpt4 key购买 nike

我正在处理本地项目并尝试连接到本地数据库。我在连接字符串中有这个类:

public class SQLConnection
{
public static string connectionString =
@"Data Source=TZVIKA-PC\SQLEXPRESS;Initial Catalog=WorldCup;Integrated Security=True";

public static WorldCupDBDataContext wcDataContext =
new WorldCupDBDataContext(connectionString);

public static WorldCupDBDataContext GetDataContextInstance()
{
return wcDataContext;
}
}

当我尝试在 Windows 窗体中执行这一行时:

 teamBindingSource.DataSource = database.Teams.OrderBy(team => team.TeamId);

我得到以下 sql 异常:

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)

我的 friend 运行了代码,它在他的电脑上运行(当然是将连接字符串更改为相关字符串时)

为什么它对我不起作用?

最佳答案

测试 SQL 连接是否正常工作的一种好(且简单)方法是使用 .UDL 文件。只需在桌面上创建一个空文本 (.txt) 文件并将扩展名重命名为 .udl。双击,您将看到一个 SQL 连接窗口,填写详细信息,测试连接,然后(当它工作时)保存。关闭窗口并将文件扩展名重命名为 .txt。打开并在里面你会找到你的工作连接字符串。

关于c# - 错误 - 尝试连接到 sql server 数据库时出现 "The server was not found or was not accessible",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com