.net中连接SQL2005出错web.config中: 
<appSettings>
    <add   key="SQLConnectionString" value="Server=(local);Database=rcenter;Uid=sa;Pwd=yanzi;" />
   </appSettings>cs代码:
try
        {            string connStr = ConfigurationSettings.AppSettings["SQLConnectionString"];
            SqlConnection myConn = new SqlConnection(connStr);
            myConn.Open();   //打开连接  
            Label1.Text = "OK!";
……运行结果:
连接出错!System.Data.SqlClient.SqlException: 无法打开登录 'rcenter' 中请求的数据库。登录失败。用户 'sa' 登录失败。 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) 在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) 在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) 在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) 在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) 在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.SqlClient.SqlConnection.Open() 在 uControl_userAdd.btnAdd_Click(Object sender, EventArgs e) 位置 e:\CS-study\home\usercontrols\UserAdd.ascx.cs:行号 31 

这是为什么呢?
我把数据库文件换成系统数据库就可以访问,没有错误。不知道是为什么:(

解决方案 »

  1.   

    看看sa 是否对rcenter 有访问权限。
      

  2.   

    数据库里的rcenter无法访问或则不存在.
      

  3.   


    您的意思是在sa的用户映射中看是否设置了对rcenter的吗?
    奇怪,我为什么设置不了,老是说:无法使用特殊主体“sa”
      

  4.   

    先检查sql server是否允许windows和sql server混合认证方式,再检查sa用户有没有登陆权限(用sql server management studio打开数据库,在安全-->登陆找到sa用户,右键点选属性,找到"状态"一栏,看允许登陆一项有没有勾上)
      

  5.   

    同意楼上的,看看Sql登录模式,和sa的权限问题。
      

  6.   

    参考
    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);
                }
            }
        }
    }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/04/30/4139145.aspx
      

  7.   

    <add  key="SQLConnectionString" value="Server=(local);Database=rcenter;Uid=sa;Pwd=yanzi;" />
    改为:<add  key="SQLConnectionString" value="Server=(local);Database=rcenter;Uid=sa;Pwd=yanzi" /> 试试