2013-04-05 95 views

回答

2

我想你可以使用Oracle.DataAccess命名空間從ODP.NET

你可以使用這樣的:

var _testConx = new OracleConnection(_testConnectionString); 
var rezList = new List<Type>(); 
string _GetSQL = @"SELECT STATEMENT"; 
var dbCommand = new OracleCommand(_GetSQL , _testConx); 
dbCommand .CommandType = CommandType.Text; 
var reader = dbCommand .ExecuteReader(); 
while (reader.Read()) 
{ 
    var rez = new Type(); 
    rez.Field1= TryGetInt(reader.GetOracleValue(0)); 
    rez.Field2= TryGetString(reader.GetOracleValue(1)); 

    rezList.Add(rez); 
} 
return rezList; 

這將使用oracle客戶端連接到遠程數據庫。

可以

+0

對不起,我不明白一步...這樣,我必須安裝客戶端Oracle(http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft-094461 html的)? – Safari 2013-04-05 18:14:58

1

我們正在使用Oracle提供的OLEDB驅動程序連接到在.NET桌面應用程序遠程Oracle數據庫指定喜歡的配置文件外部的ressource的連接字符串。應該也適用於Web服務。

String conString = "Provider=OraOLEDB.Oracle.1;User ID=username;password=password;Data Source=your_tnsname;Persist Security Info=False"; 
String query = "Select 2 from dual"; 
OleDbConnection OleDbCon = new OleDbConnection(conString); 
OleDbCon.Open(); 
OleDbCommand cmd = new OleDbCommand(query, OleDbCon); 
OleDbDataReader reader = cmd.ExecuteReader(); 
reader.Read(); 
decimal dResult = reader.GetDecimal(0); 
con.Close(); 
return Convert.ToInt32(dResult); 

您應該添加適當的異常處理。

+0

對不起,我不明白一步......這樣,我必須安裝Client Oracle(http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft-094461。 HTML)? – Safari 2013-04-05 18:16:50

1

我喜歡使用System.Data.OracleClient。我知道它已被棄用,但它內置的事實使得它很容易使用。

我也喜歡使用System.Web中的SqlDataSource對象,即使在非ASP.NET應用程序中也是如此。以下是一些示例代碼。然後,獲取數據就像調用GetDataView()並傳入select語句一樣簡單。您將需要自己實現GetDefaultConnectionString()和GetDefaultProviderName()。提供程序名稱是「System.Data.OracleClient」,並且these應該讓您開始使用連接字符串。

請注意,由於它依賴於SqlDataSource的System.Web,因此該應用程序將需要整個.NET Framework 4配置文件(不僅僅是較小的客戶端配置文件)。取決於你所做的事情,這可能是也可能不是問題。你總是可以實現你自己的SqlDataSource的等價物,但是我不想重新發明輪子,除非它給我一個很好的優勢。

/// <summary> 
    /// Creates a SqlDataSource object using the Default connectionstring in the web.config file and returns it. 
    /// </summary> 
    /// <returns>An SqlDataSource that has been initialized.</returns> 
    public static SqlDataSource GetDBConnection() 
     { 
     SqlDataSource db = new SqlDataSource(); 
     db.ConnectionString = GetDefaultConnectionString(); 
     db.ProviderName = GetDefaultProviderName(); 
     return db; 
     } 
    /// <summary> 
    /// Creates a DataView object using the provided query and an SqlDataSource object. 
    /// </summary> 
    /// <param name="query">The select command to perform.</param> 
    /// <returns>A DataView with data results from executing the query.</returns> 
    public static DataView GetDataView(string query) 
     { 
     SqlDataSource ds = GetDBConnection(); 
     ds.SelectCommand = query; 
     DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty); 
     return dv; 
     } 

進行更新/插入/刪除也同樣容易...

SqlDataSource ds=GetDBConnection(); 
ds.InsertCommand="insert into my_table values ('5','6')"; 
ds.Insert(); 
+0

對不起,我不明白一步......這樣,我必須安裝Client Oracle(http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft-094461。 HTML)? – Safari 2013-04-05 18:18:13

+1

是的,您必須使用Oracle客戶端。我相信System.Data.OracleClient和Oracle.DataAccess(ODP.NET)都需要它,因爲它們使用Oracle調用接口(oci.dll)。不確定第三方產品。這很簡單 - 只需下載客戶端,安裝它,並且您應該可以使用其中一個答案(OLEDB除外)開始。我不熟悉OLEDB。 – mason 2013-04-08 16:02:33

+0

請參閱http://geekswithblogs.net/interfacemirror/archive/2010/12/19/143176.aspx – mason 2013-04-08 16:03:13

4

我建議使用ODP.NET,因爲它是免費的,是「官方」的ADO.NET兼容用於連接提供商甲骨文。

要就無需單獨安裝Oracle客戶端,下載Oracle Instant Client,從那裏將以下文件饒你的用戶...

oci.dll 
Oracle.DataAccess.dll (the managed ODP.NET assembly itself) 
orannzsbb11.dll 
oraociei11.dll 
OraOps11w.dll 

...,並將它們與您的應用程序分發。

不幸的是,這些DLL中的大部分都是本地的(特定於32位/ 64位),因此您將無法構建「任何CPU」平臺(尚未)。您可能會考慮使用「tnsnames.ora independent」connection string如:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword; 

有商業選擇,並且現在已經過時(並且也救不了你不必反正安裝Oracle本機DLL)老微軟的供應商。

要麼等待Fully Managed Oracle Provider,或編輯您的項目文件(的MSBuild XML)有條件地包括根據構建平臺上的32位或64位的DLL,與此類似:

<Choose> 
    <When Condition="'$(Platform)' == 'x64'"> 
     <ItemGroup> 
     <Reference Include="Oracle.DataAccess, processorArchitecture=x64"> 
      <SpecificVersion>False</SpecificVersion> 
      <HintPath>..\ThirdParty\ODP.NET\x64\Oracle.DataAccess.dll</HintPath> 
     </Reference> 
     <Content Include="..\ThirdParty\ODP.NET\x64\oci.dll"> 
      <Link>oci.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x64\orannzsbb11.dll"> 
      <Link>orannzsbb11.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x64\oraociei11.dll"> 
      <Link>oraociei11.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x64\OraOps11w.dll"> 
      <Link>OraOps11w.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     </ItemGroup> 
    </When> 
    <When Condition="'$(Platform)' == 'x86'"> 
     <ItemGroup> 
     <Reference Include="Oracle.DataAccess, processorArchitecture=x86"> 
      <SpecificVersion>False</SpecificVersion> 
      <HintPath>..\ThirdParty\ODP.NET\x86\Oracle.DataAccess.dll</HintPath> 
     </Reference> 
     <Content Include="..\ThirdParty\ODP.NET\x86\oci.dll"> 
      <Link>oci.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x86\orannzsbb11.dll"> 
      <Link>orannzsbb11.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x86\oraociei11.dll"> 
      <Link>oraociei11.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     <Content Include="..\ThirdParty\ODP.NET\x86\OraOps11w.dll"> 
      <Link>OraOps11w.dll</Link> 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
     </ItemGroup> 
    </When> 
    </Choose>