连接SQL代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace DateReading
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");
            thisConnection.Open();
            SqlCommand thisCommand = thisConnection.CreateCommand();
            thisCommand.CommandText = "Select CustomerID,CompanyName from Customes";
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            while (thisReader.Read())
            {
                Console.WriteLine("\t{0}\t{1}",thisReader["CustomerID"],thisReader["CompanyName"]);
            }
            thisReader.Close();
            thisConnection.Close();
            Console.ReadKey();
        }
    }
}这样连接会提示
在建立与服务器的连接时出错。连接到SQL Server 2005时,在默认的设置下Sql server不允许进行远程连接可能会导致此失败。
哪里有问题?
new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");这里的Server=???,是不是计算机名/实例名,那么我怎么知道这个实例名呢?

解决方案 »

  1.   

    大哥,在哪里设置啊。
    第一次弄SQL,不好意思@——!
      

  2.   

    妈的,这个问题2天了,换了SQL版本为2000,以前是2005简化版的,现在还是不行,说是在配置工具-  >sql  server外围应用配置器-  >服务和连接的外围应用配置器-  >打开MSSQLSERVER节点下的Database  Engine  节点,先择  "远程连接  ",接下建议选择  "同时使用TCP/IP和named  pipes  ",确定后,重启数据库服务就可以了.  可是2000里没有这个sql  server外围应用配置器
      

  3.   

    最好换SQL帐户登陆,而不要用administrator
      

  4.   

    你设置一下SQL用户的权限试试,可以换成SA用户来登陆试试。
      

  5.   

    SqlConnection thisConnection = new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");改为:
    SqlConnection thisConnection = new SqlConnection("Server=127.0.0.1;Database=northwind;Uid=sa;Pwd=sa");Pwd=sa  sa改为你sql登陆密码
    这个在 .net2003里没问题,在 2005好象就不能这样写了
      

  6.   

    Server=???,
    是你数据库服务器名,一般是你计算机名
    也可以 用 127.0.0.1来代替
      

  7.   

    怎么感觉你的sql连接这么怪吗?
    一般都是这样写的
    Integrated Security=SSPI
    你的怎么是true?
    我没见过,汗自己一下~~~
      

  8.   

    怎么感觉你的sql连接这么怪吗?
    一般都是这样写的
    Integrated Security=SSPI
    你的怎么是true?
    我没见过,汗自己一下~~~我是按书上这么写的。
      

  9.   

    Sql的登录模式如果是Windows模式,而不是Sql server验证模式,那么可以不可以用SA帐号还登录?
    SA帐号的密码为空时,黑客可以利用这个帐号得到系统的管理员吗?
      

  10.   

    Integrated Security=True 这个是用集成win身份登陆的意思 就是说联sql服务器的时候用的 你程序运行的那个win的用户和密码不过你安装sql服务器的时候选择的是sql和win身份混合认证
    你去掉Integrated Security=True 加Uid=sa;Pwd=sa" 这个就行了不然改sql的登陆方式也行搂主好快啊 一下都学到C#入门经典24章了