2013-03-06 68 views
0

我想用asp.net連接到Oracle數據庫。任何想法如何解決當試圖連接到Oracle DB時發生「與網絡相關或發生實例特定的錯誤」?

我已經設置了正確tns.ora條目(我相信)在web.config文件,如下圖所示:

<add name="constr" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhostname)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myServiceName)));User Id=myUsername;Password=myPassword;"/> 

然後我用在連接字符串中隱藏代碼:

Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString 

我宣佈Oracle連接:

Imports System.Data.OracleClient 

首先,我得到一個錯誤,

Namespace or Type declared in Imports System.Data.OracleClient 
doesn't contain any public member or is not found 

當我刪除它,我得到了以下錯誤:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible...

認爲這與進口語句來完成。

任何想法如何解決這個錯誤?

私人小組PopulateContinents() 昏暗oOracleConn作爲的OracleConnection =新的OracleConnection() 昏暗strConnString作爲字符串= ConfigurationManager.ConnectionStrings( 「構造」)。的ConnectionString

Dim strQuery As String = "select deptID, DeptName from Dept" 
Dim con As OracleConnection = New SqlConnection(strConnString) 
Dim cmd As OracleCommand = New SqlCommand 
cmd.CommandType = CommandType.Text 
cmd.CommandText = strQuery 
cmd.Connection = con 
oOracleConn.Open() 
ddlContinents.DataSource = cmd.ExecuteReader 
ddlContinents.DataTextField = "deptID" 
ddlContinents.DataValueField = "DeptName" 
ddlContinents.DataBind() 
con.Close() 

結束子

+2

錯誤是說,它可以」 t連接到數據庫,這通常意味着您的連接字符串不正確,例如你連接到一個不存在的服務器或許多其他潛在的問題。 – 2013-03-06 18:10:07

+0

服務器確實存在。連接字符串在傳統ASP中完美工作。我調整了一點asp.net。所以,是的,服務器確實存在,連接字符串,我相信,作爲國家,是正確的。是我懷疑的進口可能沒有連接。 – 2013-03-06 18:25:11

+0

您使用的是什麼種類的連接?第二次讀取錯誤是指MS SQL連接,所以它看起來像是試圖用連接字符串詳細信息連接到MS SQL DB。發佈您正在進行連接的代碼。 – 2013-03-06 18:27:54

回答

0

其似乎該應用程序沒有得到正確的連接字符串。

首先,嘗試清除在web.config中的所有連接字符串:

<connectionStrings> 
    <clear /> 
    <add name="... 

其次,儘量指定要使用的供應商:

<add name="your.cs.name" 
    providerName="Oracle.DataAccess.Client" 
    connectionString="Data Source=//server:1521/instancename;User ID=UID;Password=PASS" /> 
+0

我是否需要安裝此提供程序?它仍然沒有工作。相同的命名空間錯誤。 – 2013-03-06 19:24:44

+0

是的,您需要Oracle提供商。 「網絡相關」錯誤是否解決? – rkawano 2013-03-06 19:36:44

+0

不,我甚至不能編譯應用程序了。 這是我的理解,您只使用Oracle 10g或更高版本的Oracle.DataAccess.Client? 我們仍在使用Oracle 9i – 2013-03-06 19:47:42

相關問題