简体   繁体   English

错误-尝试连接到SQL Server数据库时出现“找不到或无法访问服务器”

[英]Error - “The server was not found or was not accessible” when trying to connect to sql server database

I'm working on a local project and trying to connect to a local database. 我正在处理本地项目,并尝试连接到本地数据库。 I have this class in it the connection string: 我在该类中有连接字符串:

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;
    }
}

when I try to execute this line in a windows form: 当我尝试在Windows窗体中执行此行时:

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

I get the following sql exception: 我收到以下sql异常:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

My friend ran the code and it works on his pc(when changing the connection string of course to a relevant one) 我的朋友运行了该代码,并在他的PC上运行了(当将连接字符串更改为相关字符串时)

Why is it not working for me? 为什么对我不起作用?

A good (and easy) way to test if your SQL connection is working is to use a .UDL file. 测试您的SQL连接是否正常的一种好方法(简便)是使用.UDL文件。 Simply create an empty text (.txt) file on your desktop and rename the extension to .udl. 只需在桌面上创建一个空文本(.txt)文件,然后将扩展名重命名为.udl。 Double click and you'll be presented with a SQL connection window, fill in the details, test connection and then (when it's working) save. 双击,您将看到一个SQL连接窗口,填写详细信息,测试连接,然后(在工作时)保存。 Close the window and rename the file extension to .txt. 关闭窗口,然后将文件扩展名重命名为.txt。 Open and inside you'll find your working connection string. 打开并在其中找到可以使用的连接字符串。

Enable remote connections for SQL Server Express 2012 Most common problems are: 1) Your sql server express is not running. 为SQL Server Express 2012启用远程连接最常见的问题是:1)您的sql server express未运行。 Check it in "sql server configuration manager" 2) server runs but don't allow remote connection. 在“ SQL Server配置管理器”中检查它。2)服务器正在运行,但不允许远程连接。 Use sql management studio to configure it. 使用sql management studio对其进行配置。

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

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