简体   繁体   English

ADO.NET中的连接异常

[英]Exception with connection in ADO.NET

I am trying to learn ADO.NET to connect with SQL Server ... I install SQL Server with Visual Studio .... there is data base called "Northwind" as example with it ... I am trying to read this database , I wrote this code ... 我正在尝试学习ADO.NET与SQL Server的连接...我在Visual Studio中安装了SQL Server...。有一个名为“ Northwind”的数据库作为示例...我正在尝试读取此数据库,我写了这段代码...

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataSetReaderFromSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Northwind");
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "Select CustomerID , CompanyName from Customers";
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
                Console.WriteLine(reader["CustomerID"] +""+ reader["CompanyName"]);
            reader.Close();
            connection.Close(); 
        }
    }
}

When the application runs, it's take a little of time then throw exception when it trys to open connection ... the text of the exception : 当应用程序运行时,需要花费一些时间,然后在尝试打开连接时引发异常...异常文本:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

I am using Windows 7 as my operating system, and I put username on my account ... I am sure that SQL Server was installed on my computer where is my error ?? 我使用Windows 7作为操作系统,并在帐户中输入了用户名...我确定我的计算机上安装了SQL Server,这是我的错误在哪里?

Visual Studio includes Express edition of SQL Server. Visual Studio包括SQL Server的Express版本。 Thus, your connection string should most likely look like "Data Source=(local)\\sqlexpress;..." . 因此,您的连接字符串最有可能看起来像"Data Source=(local)\\sqlexpress;..."

Either

  • You haven't configured Sql Server to allow remote connections, as the error message tells you =) 您尚未将Sql Server配置为允许远程连接,因为错误消息告诉您=)
  • Your datasource is wrong. 您的数据源是错误的。 Try .\\SqlExpress or just . 尝试.\\SqlExpress. Ordinarily when Visual Studio installs Sql Server Express, it installs it as a named instance called SqlExpress. 通常,当Visual Studio安装Sql Server Express时,它将作为名为SqlExpress的命名实例安装。

If you want a quick & easy way to try different connection strings, I suggest DatabaseTester (disclaimer - I wrote it). 如果您想要一种快速简便的方法来尝试不同的连接字符串,建议您使用DatabaseTester (免责声明-我编写了它)。 It's free and a very easy way to test. 它是免费的并且非常简单的测试方法。 Also, for figuring out connections strings ConnectionStrings.com is your best friend. 另外,要弄清楚连接字符串, ConnectionStrings.com是您最好的朋友。

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

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