2016-11-24 126 views
0

我有一個名爲「Sistema的」使用應用頂點Express數據庫,我想用這個代碼連接:連接到Oracle APEX數據庫在C#

private void button1_Click(object sender, EventArgs e) 
{ 
    string constr = "Data Source=sistema;User Id=admin;Password=123;"; 
    string ProviderName = "Oracle.ManagedDataAccess.Client"; 

    using (OracleConnection conn = new OracleConnection(constr)) 
    { 
     try 
     { 
      conn.ConnectionString = constr; 
      conn.Open(); 

      //Get all the schema collections and write to an XML file. 
      //The XML file name is Oracle.ManagedDataAccess.Client_Schema.xml 
      DataTable dtSchema = conn.GetSchema(); 
      dtSchema.WriteXml(ProviderName + "_Schema.xml"); 

      MessageBox.Show("YEAH"); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      MessageBox.Show(ex.StackTrace); 
     } 
    } 
} 

這段代碼演示了我這個錯誤:

ORA-12154: TNS could not resolve the specified connection identified 

有了這個新代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     string constr = @"Data Source=(DESCRIPTION= 
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))) 
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=SISTEMA))); 
User Id=ADMIN ;Password=123"; 
     string ProviderName = "Oracle.ManagedDataAccess.Client"; 

     using (OracleConnection conn = new OracleConnection(constr)) 
     { 
      try 
      { 
       conn.ConnectionString = constr; 
       conn.Open(); 

       //Get all the schema collections and write to an XML file. 
       //The XML file name is Oracle.ManagedDataAccess.Client_Schema.xml 
       DataTable dtSchema = conn.GetSchema(); 
       dtSchema.WriteXml(ProviderName + "_Schema.xml"); 

       MessageBox.Show("YEAH"); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
       MessageBox.Show(ex.StackTrace); 
      } 
     } 
    } 

它返回我

User id 'is an invalid connection string attribute 

當我嘗試連接到由VS數據庫中我得到這個錯誤:

The listener does not currently know the requested service 

數據庫工作正常上的Oracle APEX http://localhost:8080/apex並具有表和創建 記錄也有大寫和小寫名稱,並嘗試沒有什麼變化

我不明白如何在應用程序表達式中連接到我的Oracle Apex數據庫,這很令人困惑,我不知道在oracle中正常的數據庫會發生什麼變化。

我該如何連接到我在c#中的apex數據庫?

+0

我不確定,但嘗試'User ID'而不是'User Id'並從連接字符串中刪除任何空格字符。 –

回答

0

問題是你沒有把tns標識符放在正確的地方。如果您已經從nuget添加了oracle管理數據訪問軟件包,則會在您的應用程序config/web配置中添加一個配置行。

<oracle.manageddataaccess.client> 
    <version number="*"> 
     <dataSources> 
     <dataSource alias="SampleDataSource" 
        descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)) 
        (CONNECT_DATA=(SERVICE_NAME=ORCL))) " /> 
     </dataSources> 
    </version> 
    </oracle.manageddataaccess.client> 

Edi別名爲「sistema」,並將tns連接標識符放在描述符上。像這樣

<dataSource alias="sistema" 
      descriptor="(DESCRIPTION= 
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))) 
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=SISTEMA))) " /> 
      </dataSources> 

並用你的拳頭嘗試連接字符串。這將解決TNS識別錯誤。

+0

返回「偵聽器當前不知道請求的服務」 – NemoBlack

+0

請檢查您的oracle服務器的偵聽器。在命令提示符下運行> lsnrctl服務。看看是否有「sistema」被註冊。 – yonas

+0

您也可以替換描述符部分而不是「SERVICE_NAME = SISTEMA」,您可以嘗試「SID = 」。 – yonas

0

OracleConnection無法解析別名。有根據documentation幾種可能性在下面的順序來解析名稱:在dataSources

  1. 數據源別名下在.NET配置文件<oracle.manageddataaccess.client>部(即machine.configweb.configuser.config)。

    - >這已經由約納斯在前面的回答在由TNS_ADMIN在.NET配置文件中指定的位置提供

  2. 數據源的別名在tnsnames.ora文件。位置可以由絕對或相對目錄路徑組成。
  3. tnsnames.ora文件中的數據源別名存在於與.exe相同的目錄中。

其實文檔不完全正確,或者讓我們說「不徹底」。第2項和第3項僅適用於使用本地tnsnames.ora文件。但是,在文件sqlnet.ora中,您可以指定不同的NAMES.DIRECTORY_PATH,例如,LDAPEZCONNECT

所以,實際上它應該是這樣的

  • 數據源中的tnsnames.ora文件RESP別名。在sqlnet.ora文件中定義的命名方法位於.NET配置文件中由TNS_ADMIN指定的位置。位置可以由絕對或相對目錄路徑組成。
  • 數據源別名tnsnames.ora文件resp。在sqlnet.ora文件中定義的命名方法存在於與.exe相同的目錄中。