【发布时间】:2010-07-30 09:40:52
【问题描述】:
我正在尝试学习 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();
}
}
}
当应用程序运行时,它需要一点时间然后在尝试打开连接时抛出异常...... 异常的文本:
与网络相关的或特定于实例的 建立时发生错误 连接到 SQL Server。服务器 未找到或无法访问。 验证实例名称是否为 正确,并且 SQL Server 是 配置为允许远程 连接。 (提供者:命名管道 提供者,错误:40 - 无法打开 连接到 SQL Server)
我使用 Windows 7 作为我的操作系统,我将用户名放在我的帐户中...... 我确定我的计算机上安装了 SQL Server 我的错误在哪里?
【问题讨论】:
标签: c# sql-server ado.net