#region Using Directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
#endregionnamespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection(
                @"Server=(local)\sqlexpress;Integrated Security = True;" +
                "Database=Northwind");
            thisConnection.Open();
            SqlCommand thisCommand = thisConnection.CreateCommand();
            thisCommand.CommandText =
                "select customerid,companyname from customers";
            //execute datereader for specified command
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            while (thisReader.Read())
            {
                Console.WriteLine("\t{0}\t{1}",
                   thisReader["customerid"], thisReader["companyname"]);
            }
            thisReader.Close();
            thisConnection.Close();
            Console.ReadLine();        }
    }
}
以上代码老是连不上SQL 2005 ,提示问题在thisConnection.Open();原因是在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
但是我已经开通了 SQL 2005的 TCP/IP 和 named pipes(b)服务 

解决方案 »

  1.   

    连接字符串有问题,在服务管理器中先建立连接后点数据库的右键后把它的连接字符串copy出来贴到你的代码里
      

  2.   

    用这个看看
    "server=.;uid=sa;pwd=***;database=Northwind"
      

  3.   

    碰到过这种情况,
    考虑下数据库是否是自己创建的还是从别处拷贝过来的。
    如果是自己创建的,将连接字符串中的Server改为:@"Server=.\sqlexpress;...试试;
    如果是从别处拷贝的,有可能存在版本及权限问题,涉及到转换
      

  4.   

    我想可能是我的SQL 2005不是express版本的,是正常版的原因吧, 不知道:@"Server=.\sqlexpress;...其中的sqlexpress改成什么好
      

  5.   

    如果是混合连接应该用"server=.;uid=sa;pwd=***;database=Northwind"。
    如果是本地连接,建议用混合的。sp_password   null,'newpassword','sa'。
      

  6.   

    先用odbc 连下 基本上是因为2005安装时候默认已经关闭了远程连接 需要将用户的远程连接打开 再将
    SQL 外围应用配置器关闭后再 启动即可
      

  7.   

    @"Server=(local)\sqlexpress;Integrated Security = True;" + 呵呵不是这个原因,我去掉其中的\sqlexpress 就正常了,因为我的不是express版本的,
    正确的应该是@"Server=(local);Integrated Security = True;" +