如何从 .NET 连接到 Oracle DB?
How to connect to Oracle DB from .NET?
当我打开 SQL 命令行时,我输入 CONNECT username/password@[//]host[:port][/service_name]
它就可以很好地将我连接到数据库。但是,我无法使用连接字符串从 .NET 项目进行连接。我已经尝试了很多东西,例如 <add name="ConnectionString" connectionString="Data Source=username/password@[//]host[:port][/service_name];" />
和 <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />
,但到目前为止没有任何效果。每当我在下面到达 sconn.Open();
时:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
我几乎总是遇到以下错误:
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: SQL
Network Interfaces, error: 25 - Connection string is not valid)
如何正确连接数据库?
您始终可以使用 EF,并使用 create one ADO.Net Entity Data Model
并使用向导创建和测试连接
尝试以下连接字符串
string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";
& 然后使用此连接字符串如下
using (OracleConnection objConn = new OracleConnection(con))
{
\ code
}
您使用 System.Data.SqlClient.SqlConnection
用于连接到 (Microsoft) SQL 服务器,它不能用于 Oracle 连接。
您应该使用 System.Data.OracleClient.OracleConnection
或 Oracle.ManagedDataAccess.Client.OracleConnection
查看此答案以了解其他可能性:
我强烈推荐使用 "Official Oracle ODP.NET, Managed Driver"。
https://www.nuget.org/packages/Oracle.ManagedDataAccess/
或 Entity framework 的那个:
https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/
通过 Visual Studio 安装后,它会打开一个自述文件,我也建议您阅读。
Oracle 提供了海量的文档。从这里开始:
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
当我打开 SQL 命令行时,我输入 CONNECT username/password@[//]host[:port][/service_name]
它就可以很好地将我连接到数据库。但是,我无法使用连接字符串从 .NET 项目进行连接。我已经尝试了很多东西,例如 <add name="ConnectionString" connectionString="Data Source=username/password@[//]host[:port][/service_name];" />
和 <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />
,但到目前为止没有任何效果。每当我在下面到达 sconn.Open();
时:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
我几乎总是遇到以下错误:
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: SQL Network Interfaces, error: 25 - Connection string is not valid)
如何正确连接数据库?
您始终可以使用 EF,并使用 create one ADO.Net Entity Data Model
并使用向导创建和测试连接
尝试以下连接字符串
string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";
& 然后使用此连接字符串如下
using (OracleConnection objConn = new OracleConnection(con))
{
\ code
}
您使用 System.Data.SqlClient.SqlConnection
用于连接到 (Microsoft) SQL 服务器,它不能用于 Oracle 连接。
您应该使用 System.Data.OracleClient.OracleConnection
或 Oracle.ManagedDataAccess.Client.OracleConnection
查看此答案以了解其他可能性:
我强烈推荐使用 "Official Oracle ODP.NET, Managed Driver"。
https://www.nuget.org/packages/Oracle.ManagedDataAccess/
或 Entity framework 的那个: https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/
通过 Visual Studio 安装后,它会打开一个自述文件,我也建议您阅读。
Oracle 提供了海量的文档。从这里开始: http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html