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

标签 c# sql-server

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

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/

相关文章:

sql-server - Azure - 使用本地 (Azure) IP 的 Web 到 SQL

sql - SQL Server 列的瀑布逻辑

sql - MSSQL 更新查询将结果显示到网格

c# - 如何在C#(ASP.Net)中调用参数化存储过程?

c# - 如何通过 Protobuf/Protobuf-net 使用某些对象继承的列表/数组?

sql-server - 从SQL Server触发器调用Powershell脚本

c# - 尝试使用 C# ado,net 将行插入到 sql server 数据库

c# - 就大小而言,最好的对称 .net 加密提供程序是什么?

c# (WinForms-App) 将数据集导出到 Excel

c# - Entity Framework 和 WPF 数据网格绑定(bind)