System.Data.EntityException:基础提供程序在Open上失败

时间:2014-04-13 21:09:31

标签: c# winforms entity-framework sql-server-2008-r2

我正在构建一个在服务器计算机上有数据库的应用程序,我正在使用此方法进行数据库访问:

public static string GetDefaultConnectionString()
{
    //get my db info from ini file
    dbinfo DatabaseInfo = new dbinfo();
    DatabaseInfo = GetDatabaseInfo();
    if (DatabaseInfo.dbName == "" || DatabaseInfo.Password == "" || DatabaseInfo.Server == "" || DatabaseInfo.UserId == "")
    {
        throw new Exception("Database config file is not valid");
    }
    else
    {
        MessageBox.Show("Db name " + DatabaseInfo.dbName);
        MessageBox.Show("User " + DatabaseInfo.UserId);
        MessageBox.Show("Db Pass " + DatabaseInfo.Password);
        MessageBox.Show("Db Server " + DatabaseInfo.Server);
    }
    string conStrIntegratedSecurity = new System.Data.EntityClient.EntityConnectionStringBuilder
    {
        Metadata = "res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl",
        Provider = "System.Data.SqlClient",
        ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
        {
            InitialCatalog = DatabaseInfo.dbName,
            DataSource = DatabaseInfo.Server,
            IntegratedSecurity = true,
            UserID = DatabaseInfo.UserId,
            Password = DatabaseInfo.Password,
            MultipleActiveResultSets = true,
            ConnectTimeout = 0,
            MaxPoolSize = 500,
        }.ConnectionString
    }.ConnectionString;
    //MessageBox.Show(conStrIntegratedSecurity);
    return conStrIntegratedSecurity;
}

它在服务器电脑上工作正常。 但是,当我在任何客户端计算机上安装此应用程序时,它无法打开连接,它会给出以下错误消息:

System.Data.EntityException: The underlying provider failed on Open. ---> 
System.Data.SqlClient.SqlException: Cannot open database "dbName" requested by the login. The login failed.
Login failed for user 'ServerName\User'.

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

我只是用

 IntegratedSecurity = true,

 IntegratedSecurity = false,

它有效!