繁体   English   中英

使用ADO.NET连接到SQL Azure

[英]Connecting to SQL Azure with ADO.NET

嗨,我尝试使用ADO.NET连接到SQL AZURE DB。

这是我的代码:

private static string userName = "<**@********>";
private static string password = "<********>";
private static string dataSource = "<******.database.windows.net>";
private static string databaseName = "<******>";

public void Save()
{
    SqlDataReader queryResultCloud;
    string queryString = "select * from tblScan";


    SqlConnectionStringBuilder connString2Builder;
    connString2Builder = new SqlConnectionStringBuilder();
    connString2Builder.DataSource = dataSource;
    connString2Builder.InitialCatalog = databaseName;
    connString2Builder.Encrypt = true;
    connString2Builder.TrustServerCertificate = false;
    connString2Builder.UserID = userName;
    connString2Builder.Password = password;

    using (SqlConnection connection = new SqlConnection(connString2Builder.ConnectionString))
    {
        SqlCommand command = new SqlCommand(queryString);
        command.Connection = connection;
        connection.Open();

        queryResultCloud = command.ExecuteReader();
        connection.Close();
    }

我得到下一个错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

您的代码似乎是正确的。 可以尝试的一件事是将TrustServerCertificate设置为true,然后查看它是否有效。 请注意,当Encrypt设置为true时,建议将此属性设置为false。 但是有报道说这种组合会导致连接问题。 我建议您检查http://www.wadewegner.com/2010/08/using-the-trustservercertificate-property-with-sql-azure-and-entity-framework/了解更多信息。 此外,再次检查是否已配置SQL Azure防火墙以允许连接到本地计算机。 如果您位于代理后面,则需要将代理的IP地址添加到防火墙例外中。

最好的祝福,

徐明

您可能位于不允许端口1433出站连接的代理后面,这是您可以与SQL Azure一起使用的唯一端口

Windows Azure SQL数据库服务仅在TCP端口1433上可用。要从计算机访问SQL数据库数据库,请确保防火墙允许TCP端口1433上的传出TCP通信。

我也处于这种情况下,最有前途/最具挑战性的解决方案似乎是此端口桥接应用程序技术 ,但是该解决方案本身已过时,并且需要比我已安装的VS更旧的版本,因此我正在研究一些其他选择。

暂无
暂无

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

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