我现在用的是VS2005+SQL Server2000开发WIN程序,可现在边数据库还没有连上,急呀!
代码:
string connString = "Server=localhost;Database=UserDB;User ID=sa;Password=;";
string selectString = "select username,password,usertype from User";
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(selectString, conn);
command.Connection.Open();//总是执行到此处出现错误
SqlDataReader dr = command.ExecuteReader();
错误:
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)

解决方案 »

  1.   

    password后面那个;似乎是不需要的
      

  2.   

    http://www.connectionstrings.com/
    "Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;" 
       - or -
    "Server=Aron1;Database=pubs;User ID=sa;Password=asdasd;Trusted_Connection=False" 
       (both connection strings produces the same result)
      

  3.   

    "Server=localhost;Database=UserDB;User ID=sa;Password=;Trusted_Connection=False" conn.Open();
      

  4.   

    你的电脑肯定是连了网的, 把连接字符串中Server=localhost 改成
    Server=.试试
      

  5.   

    用SqlConnection控件得了,免得把ConnectionString写错
      

  6.   

    在这里 http://community.csdn.net/Expert/topic/4922/4922102.xml?temp=.6234552
      

  7.   

    你的SQL Server的登录验证模式是Windows的还是Sql、Windows混合模式的?
      

  8.   

    很有可能与你的MSSQLSERVER的验证模式有关,你可以打开“管理工具”-“服务”,找到“MSSQLSERVER”,将验证模式改为SQL模式试试。
      

  9.   

    感觉Server=localhost有问题,楼主的Server Server名是叫localhost?
      

  10.   

    我前一段时间做过一个WEB程序,也是这样的连接字符串就可以,为啥这个就不可以了呢?
    Server=localhost就是连接到本地的服务呀,这个有什么问题呢?
      

  11.   

    Server=localhost 
    把localhost 换成你SQL SERVER 服务器名字
      

  12.   

    Server=localhost 
    把localhost换成你的IP地址试试看
      

  13.   

    请问楼住会不会用Web.config阿!如果会的话!我给你个代码!
      

  14.   

    应该是登录模式的问题,我还问过同样的问题可没人给我解答.
    郁闷,我就放弃用sqlserver2005 而改用Oracle和sqlserver2000了
     有一个能进配置的东西 在vs里 试下看什么毛病
      

  15.   

    把localhost改成你那个SQl server服务器的具体名字
      我上次也出现过这样的情况,后来改了就好了
      

  16.   


    "换成conn.Open();也是一样的!"
    "SQL Server的登录验证模式是Windows的还是Sql、Windows混合模式的?"不过一般情况只要是使用过SQL的人都会选择Windows混合模式的.conn.Open();和问题者写的其实都是一样的.没什么区别问题极大的可能是出在以下
    string connString = "Server=localhost;Database=UserDB;User ID=sa;Password=;";中的Server=localhost;你的数据库服务器应该是Microsoft SQL(看你所使用的怎么样的SQL),而不是localhost.
      

  17.   

    你的sql配置工具里的设置是不是正确的
      

  18.   

    连接字符串错了Server=localhost 改成
    Server=(local) 或者你的机器名如果你装的 是 sql server 2005 expressServer=(local)\SQLEXPRESS 或者 机器名\SQLEXPRESS
      

  19.   

    SQL配置错误,以前我也遇到过的~
    你把SQL里的权限配置一下
      

  20.   

    或者把连接字符串改为:
    server=(local)\\UserDB;integrated security=sspi;database=表名
      

  21.   

    bool isValue = false;
    SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=;database=DbName;"
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * from table",conn);
    SqlDataReader myDR = cmd.ExecuteReader();isValue = myDR.Read();
      

  22.   

    SqlConnection con;
                string constr =sectest.Properties.Settings.Default.st_scoreConnectionString;
                con = new SqlConnection(constr);
                con.Open();
                SqlDataAdapter myadapter;
                string cmd = "SELECT * FROM  " + comboBox1.Text + "";
                myadapter = new SqlDataAdapter(cmd, con);
                myadapter.Fill(ds, comboBox1.Text);
                dataGridView1.DataSource = ds.Tables[comboBox1.Text];
                con.Close();
    我做数据库时用的代码,可以参考一下
    数据库可以先用界面的方式连接后存在sectest.Properties.Settings.Default.st_scoreConnectionString;中
      

  23.   

    User是SQL内带的
    最好不要用这个表名
    实在要用的话加中括号,改成
    select [username],[password],usertype from [User]
      

  24.   

    先把其他的去掉,只保留到conn.Open(),看有没有错误,如果没错,则连接没问题,是SQL语句有问题。按步排除错误。
      

  25.   

    1.localhost改成
    .\sqlexpress
    2.sql是不是“混合验证”,如果不是sql不能登录
      

  26.   

    SqlConnection con=new SqlConnection("server=.;database=数据库名;uid=sa;pwd=;");
    复制我的代码砍下!
      

  27.   

    注意大小写
    我给你改了下代码
    string connString = "server=.;database=UserDB;uid=sa;pwd=;";
    string selectString = "select username,password,usertype from User";
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();
    SqlCommand command = new SqlCommand(selectString, conn);
    SqlDataReader dr = command.ExecuteReader();你复制砍下!
      

  28.   

    是连接字符才串的问题,你用Web.config配置一下
      

  29.   

    Server=localhost;Database=UserDB;User ID=sa;Password=;" 
    localhost改成local吧
      

  30.   

    将localhost改成(local)或者改成你的机器IP试试
    还有打开数据库改成 :conn.open();
      

  31.   

    用sa能登陆sql查询分析起吗?你先检查一下吧
    再有,你的超级管理员邮密码吗?
      

  32.   

    谢谢大家的回答,问题已经解决了,有可能是Server=localhost的问题,原来的服务名不是计算机名,我原来的SQLSERVER删了,重装一次,这次服务器名为默认的计算机名就只可以了,希望大家以后要注意这个问题!