How to connect to a remote Oracle database

邮差的信 提交于 2020-01-03 15:56:14

问题


I have to connect to a remote Oracle DBMS into my .NET C# web service

  • Is request the client Oracle installation? Why?
  • When you have to use ODP.NET

Thanks


回答1:


I recommend using ODP.NET, as it's free and is the "official" ADO.NET compatible provider for connecting to Oracle.1

To spare your users from having to separately install the Oracle Client, download the Oracle Instant Client, take the following files from there...

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

...and distribute them with your application.

Unfortunately, most of these DLLs are native (and 32-bit / 64-bit specific), so you won't be able to build for "Any CPU" platform (yet2).

The .NET code is identical to what you would use under "fat" Oracle Client (and is very similar to any other ADO.NET provider out there), except you should probably consider using "tnsnames.ora independent" connection string such as:

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

1 There are commercial alternatives and the old Microsoft's provider that is now deprecated (and won't save you from having to install Oracle native DLLs anyway).

2 Either wait for Fully Managed Oracle Provider, or edit your project file (the MSBuild XML) to conditionally include 32-bit or 64-bit DLLs depending on the build platform, similar to this:

  <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>



回答2:


I think you can use the Oracle.DataAccess namespace from ODP.NET

You can use like this:

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;

This will use the oracle client to connect to Remote Database.

You can specify the connection string in external ressource like config file




回答3:


We are using the OLEDB drivers provided by Oracle to connect to a remote Oracle Database in a .net desktop application. Should work for web services as well.

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);

You should add proper exception handling.




回答4:


I like to use System.Data.OracleClient. I know it's deprecated, but the fact that it's built in makes it dead simple to use.

I also like to make use of the SqlDataSource object from System.Web, even in non ASP.NET applications. Below is some example code. Then, getting the data is as simple as calling GetDataView() and passing in your select statement. You will need to implement GetDefaultConnectionString() and GetDefaultProviderName() yourself. The provider name is "System.Data.OracleClient", and these should get you started with the connection string.

Note that since it depends on System.Web for SqlDataSource, the application will require the entire .NET Framework 4 profile (not just the smaller Client Profile). Depending on what you're making, this may or may not be a problem. You could always implement your own equivalent of SqlDataSource, but I prefer not to reinvent the wheel unless it gives me a nice advantage.

    /// <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;
        }

Making updates/inserts/deletes is similarly easy...

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


来源:https://stackoverflow.com/questions/15834561/how-to-connect-to-a-remote-oracle-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!