我需要连接到远程Oracle DBMS来使用我的.NET C# web服务
- 客户端需要安装Oracle吗?为什么?
- 何时需要使用ODP.NET
谢谢
我需要连接到远程Oracle DBMS来使用我的.NET C# web服务
谢谢
我建议使用ODP.NET,因为它是免费的,并且是连接到Oracle的“官方”ADO.NET兼容提供程序。1
为了让您的用户不必单独安装Oracle Client,请下载Oracle Instant Client,从中取出以下文件...
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提供程序非常相似),除了您可能考虑使用“tnsnames.ora独立”连接字符串,例如:
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 有商业替代品以及旧的Microsoft提供程序,现在已经不推荐使用(无论如何也无法避免安装Oracle本地DLL)。
2 要么等待完全托管的Oracle提供程序,要么编辑项目文件(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>
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客户端连接到远程数据库。
您可以在外部资源(如配置文件)中指定连接字符串。
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。我知道它已经过时了,但它内置的事实使得它非常容易使用。
我也喜欢在非 ASP.NET 应用程序中使用 System.Web 的 SqlDataSource 对象。以下是一些示例代码。然后,只需调用 GetDataView() 并传入您的选择语句,即可轻松获取数据。您需要自己实现 GetDefaultConnectionString() 和 GetDefaultProviderName()。提供程序名称为 "System.Data.OracleClient",这些 可以帮助您开始使用连接字符串。
请注意,由于它依赖于 System.Web 用于 SqlDataSource,因此应用程序将需要整个 .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();