简体   繁体   English

使用OleDbConnection连接到SQL失败

[英]Connect to SQL using OleDbConnection failing

I'm connecting to a remote SQL database using the OleDbConnection but it's failing. 我正在使用OleDbConnection连接到远程SQL数据库,但是失败了。

var connection = new OleDbConnection
{
    ConnectionString = "Provider=SQLOLEDB;server=MyRemoteIPAddress;Initial Catalog=MyDatabase;User ID=sa;Password=MyPassword"
};

try
{
    connection.Open();
    connection.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

The error I am getting is : [DBNETLIB][ConnectionOpen (Invalid Instance()).]Invalid connection. 我遇到的错误是:[DBNETLIB] [ConnectionOpen(无效的Instance())。]无效的连接。

However if I connect using SqlConnection, it connects ok. 但是,如果我使用SqlConnection连接,则连接正常。

var testConnection = new SqlConnection
{
    ConnectionString = "server=MyIPAddress;database=MYDatabase;user id=MyUser;pwd=MyPassword"
};

try
{
    testConnection.Open();
    testConnection.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

Why can i connect via SqlConnection and not OleDbConnection? 为什么我可以通过SqlConnection而不是OleDbConnection进行连接?

The environment is not clustered but explicitly specifying the port fixed the issue. 该环境不是群集的,而是明确指定端口来解决此问题。

This worked : 这工作:

"Provider=SQLOLEDB;server=MyRemoteIPAddress,1433;Initial Catalog=MyDatabase;User ID=sa;Password=MyPassword" “提供程序= SQLOLEDB;服务器= MyRemoteIPAddress,1433;初始目录= MyDatabase;用户ID = sa;密码= MyPassword”

Try using a server side code which does all the required functions. 尝试使用执行所有必需功能的服务器端代码。 so that your application does not directly talk with the remote database. 这样您的应用程序就不会直接与远程数据库通信。 Ping me for further assistance. 请我进一步寻求帮助。

you should specify the port 1433 when using OLEDB 使用OLEDB时应指定端口1433

Provider=SQLOLEDB;server=MyRemoteIPAddress,1433;Initial 
Catalog=MyDatabase;User ID=sa;Password=MyPassword;

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

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