C# 如何连接到远程Oracle数据库
我必须将远程Oracle DBMS连接到.NET C web服务中 请求客户端是Oracle安装吗?为什么? 当您必须使用ODP.NET时 谢谢我认为您可以使用ODP.NET中的Oracle.DataAccess名称空间 您可以这样使用:C# 如何连接到远程Oracle数据库,c#,.net,visual-studio-2010,oracle,odp.net,C#,.net,Visual Studio 2010,Oracle,Odp.net,我必须将远程Oracle DBMS连接到.NET C web服务中 请求客户端是Oracle安装吗?为什么? 当您必须使用ODP.NET时 谢谢我认为您可以使用ODP.NET中的Oracle.DataAccess名称空间 您可以这样使用: var _testConx = new OracleConnection(_testConnectionString); var rezList = new List<Type>(); string _GetSQL = @"SELECT STATE
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客户端连接到远程数据库
您可以在外部资源(如配置文件)中指定连接字符串。我们正在使用Oracle提供的OLEDB驱动程序连接到.net桌面应用程序中的远程Oracle数据库。应该也适用于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);
您应该添加适当的异常处理。我喜欢使用System.Data.OracleClient。我知道它已经被弃用了,但事实上它是内置的,使用起来非常简单 我还喜欢使用System.Web中的SqlDataSource对象,即使在非ASP.NET应用程序中也是如此。下面是一些示例代码。然后,获取数据就像调用GetDataView并传入select语句一样简单。您需要自己实现GetDefaultConnectionString和GetDefaultProviderName。提供程序名称为System.Data.OracleClient,您应该从连接字符串开始 请注意,由于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;
}
我建议使用ODP.NET,因为它是免费的,并且是连接到Oracle.1的官方ADO.NET兼容提供程序 为了避免您的用户需要单独安装Oracle客户端,请下载,从中获取以下文件
oci.dll
Oracle.DataAccess.dll (the managed ODP.NET assembly itself)
orannzsbb11.dll
oraociei11.dll
OraOps11w.dll
…并将其与应用程序一起分发
不幸的是,大多数DLL都是本机的,并且是32位/64位特定的,因此您将无法为任何CPU平台2构建
.NET代码与您在FAT Oracle客户端下使用的代码相同,并且与其他任何ADO.NET提供程序非常相似,除了您可能应该考虑使用TnNAMES.OLA独立性,例如:
1有一些商业替代方案和旧的Microsoft提供程序,它们现在已被弃用,无论如何都不会使您免于安装Oracle本机DLL 2根据生成平台,等待或编辑项目文件MSBuild XML,使其有条件地包含32位或64位DLL,如下所示:对不起,我一步也不懂。。。这样,我必须安装客户端Oracle吗?对不起,我不明白一个步骤。。。这样,我必须安装客户端Oracle吗?对不起,我不明白一个步骤。。。这样,我必须安装Oracle客户端吗?是的,您必须使用Oracle客户端。我相信System.Data.OracleClient和Oracle.DataAccess ODP.NET都需要它,因为它们使用Oracle调用接口oci.dll。不确定第三方产品。这很简单,只要下载客户端,安装它,你就可以使用这些答案中的一个,除了OLEDB来开始。我不熟悉OLEDB。
oci.dll
Oracle.DataAccess.dll (the managed ODP.NET assembly itself)
orannzsbb11.dll
oraociei11.dll
OraOps11w.dll
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
<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>