您好我尝试使用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)
答案 0 :(得分:0)
您的代码似乎是正确的。要尝试的一件事是将TrustServerCertificate设置为true,并查看它是否有效。请注意,当Encrypt设置为true时,建议将此属性设置为false。但据报道,这种组合会导致连接问题。我建议您查看http://www.wadewegner.com/2010/08/using-the-trustservercertificate-property-with-sql-azure-and-entity-framework/以获取更多信息。此外,再次检查是否已配置SQL Azure防火墙以允许连接到本地计算机。如果您在代理服务器后面,则需要将代理服务器的IP地址添加到防火墙例外。
最诚挚的问候,
徐明。答案 1 :(得分:0)
您可能在代理后面不允许到端口1433的出站连接,这是唯一的port you can use with SQL Azure:
Windows Azure SQL数据库服务仅适用于TCP端口 1433.要从计算机访问SQL数据库数据库,请确保防火墙允许TCP端口1433上的传出TCP通信。
我也处于这种情况,最有希望/最有挑战性的解决方案似乎是这个port bridging application technique,但解决方案本身已过时,需要比我安装的旧版VS,所以我'我正在寻找其他一些替代方案。