如何从.NET连接到Oracle DB?
当我打开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();
:
When I open SQL Command Line, I write CONNECT username/password@[//]host[:port][/service_name]
and it connects me to the database just fine. However, I'm unable to connect from a .NET project using a connection string. I have tried many things such as <add name="ConnectionString" connectionString="Data Source=username/password@[//]host[:port][/service_name];" />
and <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />
, but nothing worked so far. Whenever I get to sconn.Open();
in the following:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
我几乎总是收到以下错误:
I practically always get the following error:
与网络相关或特定于实例的错误发生在 建立与SQL Server的连接.找不到服务器或 无法访问.验证实例名称正确,并且 SQL Server配置为允许远程连接. (提供者:SQL 网络接口,错误:25-连接字符串无效)
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)
如何正确连接数据库?
尝试使用以下连接字符串
Try following connection string
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";
&然后按如下所示使用该连接字符串
& then use this conection string as follow
using (OracleConnection objConn = new OracleConnection(con))
{
\\ code
}