2016-03-04 132 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