2016-03-04 92 views
3

当我打开SQL命令行时,我写了CONNECT username/[email protected][//]host[:port][/service_name],它将我连接到数据库就好了。但是,我无法使用连接字符串从.NET项目进行连接。我尝试了很多东西,例如<add name="ConnectionString" connectionString="Data Source=username/[email protected][//]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();在以下几点:如何从.NET连接到Oracle DB?

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)

我怎样才能连接到数据库是否正常?

+1

http://www.connectionstrings.com/oracle/ – Plutonix

+0

服务器可能被配置为不允许远程连接。这在所有主要的RDBMS服务器,MSSQL,PostgreSQL,MySql中都是一样的,我想Oracle也是如此。 如果它不允许远程连接,那么即使连接字符串正确,它也不可访问。 –

回答

1

尝试下面的连接字符串

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 
} 
+0

OracleException:在.NET标准类库中使用它时无法分配Oracle环境 –

0

您可以随时使用EF,并使用创建一个ADO.Net Entity Data Model

enter image description here

,并使用向导来创建和测试连接

enter image description here

1

您使用System.Data.SqlClient.SqlConnection这是用来连接到(微软)SQL服务器,它不能用于Oracle连接。

您应该使用System.Data.OracleClient.OracleConnectionOracle.ManagedDataAccess.Client.OracleConnection

看一看这个答案也能看到其他的可能性: How to connect to Oracle 11 database from . net