简体   繁体   English

实体框架连接字符串使能连接到DB服务器

[英]Entity framework connection string enable to connect to DB server

I'm using the entity framework in a winforms application. 我在winforms应用程序中使用实体框架。

When i set scsb.DataSource ="localhost" every thing works fine but when i try to connect to onother DB server i got an exception: 当我设置scsb.DataSource =“localhost”时,每件事都运行正常,但当我尝试连接到另一台DB服务器时,我得到一个例外:

" The underlying provider failed on Open ." 底层提供商在Open上失败了 。”

public DistributionSSEntities1 Connection()
{
    var scsb = new SqlConnectionStringBuilder();
    scsb.DataSource = "192.168.1.100";
    scsb.InitialCatalog = "DistributionSS";
    scsb.IntegratedSecurity = true;
    //------------------------
    EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
    builder.Metadata ="res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl";
    builder.Provider = "System.Data.SqlClient";
    builder.ProviderConnectionString = scsb.ConnectionString;
    DistributionSSEntities1 db = new DistributionSSEntities1(builder.ToString());
    return db;
}

Has the remote Sql been setup to allow remote connections? 远程Sql是否已设置为允许远程连接? Has the remote Sql been allowed access through the windows firewall... there's so many reasons why it wouldn't connect. 是否允许远程Sql通过Windows防火墙进行访问...有很多原因导致它无法连接。

You're using Integrated Security - which may work great for a local Sql; 您正在使用集成安全性 - 这可能适用于本地Sql; but the network user that your WinForm app is running under must have the correct rights to access the remote box. 但运行WinForm应用程序的网络用户必须具有访问远程复选框的正确权限。

I'd suggest to start eliminating possibilities do the following: 我建议开始消除可能性如下:

  1. Check the Sql logs on the target server. 检查目标服务器上的Sql日志。 That always has the exact reason why an attemp failed - not the watered down version you get through the exception. 这总是有一个尝试失败的确切原因 - 而不是你通过例外的淡化版本。 (eg. C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLEXPRESS\\MSSQL\\Log) (例如,C:\\ Program Files \\ Microsoft SQL Server \\ MSSQL11.SQLEXPRESS \\ MSSQL \\ Log)

  2. Connect to it using a sql username password - not integrated security to make sure it's not that 使用sql用户名密码连接到它 - 没有集成的安全性,以确保它不是那样

  3. Firewall 火墙

EDIT 编辑

It's important to remember that the error messages return to the client regarding login attempt failures are purposefully obscure or without information - to limit an attacker gaining enough information to improve the attack (see the technet article for proof). 重要的是要记住,有关登录尝试失败的错误消息返回客户端是有目的地模糊或没有信息 - 限制攻击者获得足够的信息来改善攻击(请参阅technet文章以获取证据)。 So checking the Sql Server logs is a necessity - if your login/connection attempt actually made it to the server. 因此,检查Sql Server日志是必要的 - 如果您的登录/连接尝试实际上已将其发送到服务器。

From Article: 来自文章:

To increase security, the error message that is returned to the client deliberately hides the nature of the authentication error. 为了提高安全性,返回给客户端的错误消息故意隐藏了身份验证错误的性质。 However, in the SQL Server error log, a corresponding error contains an error state that maps to an authentication failure condition. 但是,在SQL Server错误日志中,相应的错误包含映射到身份验证失败条件的错误状态。 Compare the error state to the following list to determine the reason for the login failure. 将错误状态与以下列表进行比较,以确定登录失败的原因。

 public DistributionSSEntities Connection()
    {
        string ConString = "SERVER=192.168.1.100;DATABASE=DistributionSS;UID=sa;PASSWORD=125;";
        SqlConnectionStringBuilder SCB= new SqlConnectionStringBuilder(ConString);
        //------------------------
        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
        builder.Metadata = "res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl";
        builder.Provider = "System.Data.SqlClient";
        builder.ProviderConnectionString = SCB.ConnectionString;
        DistributionSSEntities db = new DistributionSSEntities(builder.ToString());
        return db;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM