private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"server=.;Integrated Security=True;Data Source=mydataBase1;");
            con.Open();
            //try
            //{
                SqlCommand cmd = new SqlCommand("select * from users where userName='"+this.txtUser.Text.Trim()+"' and userPwd='"+this.txtPwd.Text.Trim()+"'",con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    MessageBox.Show("登陆成功!!!");
                }
                else
                {
                    MessageBox.Show("登陆失败!!!");
                }
                con.Close();
            //}
            //catch(Exception e1)
            //{
            //    MessageBox.Show("连接数据库失败!");
            //}        }错误:停止到con.open();那行
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) 

解决方案 »

  1.   

    连接字符串有错误吧?
    Data Source=127.0.0.1;Initial Catalog=db_tttt;Persist Security Info=True;User ID=sa;Password=yyyyyyyy
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // "uid=sa":连接数据库的用户名为sa.
                // "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
                // "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
                // "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".
                // " Connect Timeout=30":连接超时时间为30秒.(根据情况添加)
                // PS:
                //  1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
                //  2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
                string strConnection = "Trusted_Connection=SSPI;";
                strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;";
                strConnection += "Connect Timeout=30";
                using (SqlConnection objConnection = new SqlConnection(strConnection))
                {
                    objConnection.Open();
                    // method 1
                    SqlCommand myCommand = new SqlCommand("select * from Couse", objConnection);                Console.WriteLine("open");
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    while (myReader.Read())
                    {
                        Console.WriteLine(myReader.GetFieldType(0));
                        Console.WriteLine(myReader.GetFieldType(1));
                        Console.WriteLine(myReader.GetFieldType(2));
                        Console.WriteLine(myReader.GetDecimal(0));
                        Console.WriteLine(myReader.GetDecimal(1));
                        Console.WriteLine(myReader.GetDecimal(2));
                    }
                    myReader.Close();                // method 2
                    SqlDataAdapter myCommandd = new SqlDataAdapter("select * from Couse", objConnection);
                    DataSet ds = new DataSet();
                    myCommandd.Fill(ds, "Couse");
                    DataTable dt = ds.Tables["couse"];
                    Console.WriteLine(dt.Columns[0].ToString());
                    Console.WriteLine(dt.DefaultView.Count);
                }
            }
        }
    }
      

  3.   

    这种绝对行!1.打开服务器资源管理器,在那添加连接。
    2.输入计算机名\SQLEXPRESS,然后找到你要连接的数据库。
    3.单击连接那里找到连接字符串然后粘上去就行了。