下面演示了使用OleDb, msdaora, System.Data.OracleClient.OracleConnection, Oracle.DataAccess.Client.OracleConnection 以及用DbProviderFactories.GetFactory方式来获取Connection
另外如果是64位操作系统,需要安装64位的驱动
代码如下:
using System.Data;
using System.Data.Common;
namespace WinTest
{
class Program
{
static void Main(string[] args)
{
ShowAllDbProviderFactories();
string connString;
/* ole db provider */
connString = "Provider=OraOledb.Oracle;Data Source=xe;User ID=sa;Password=jack2008;";
using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
{
TestConnection(conn);
}
connString = "Provider=OraOleDb.Oracle.1;Data Source=xe;User ID=sa;Password=jack2008;";
using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
{
TestConnection(conn);
}
/* ms oracle provider, it is x86 version */
//connString = "Provider=msdaora;Data Source=xe;User ID=sa;Password=jack2008;";
//using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
//{
// TestConnection(conn);
//}
connString = "Data Source=xe;User ID=sa;Password=jack2008;";
using (DbConnection conn = new System.Data.OracleClient.OracleConnection(connString))
{
TestConnection(conn);
}
connString = "Data Source=xe;User ID=sa;Password=jack2008;";
using (DbConnection conn = new Oracle.DataAccess.Client.OracleConnection(connString))
{
TestConnection(conn);
}
string providerInvariantName;
providerInvariantName = "Oracle.DataAccess.Client";
connString = "Data Source=xe;User ID=sa;Password=jack2008;";
UseDbProviderFactoryGetConnection(providerInvariantName,connString);
providerInvariantName = "System.Data.OracleClient";
connString = "Data Source=xe;User ID=sa;Password=jack2008;";
UseDbProviderFactoryGetConnection(providerInvariantName, connString);
//connString = "Data Source=xe;User ID=sa;Password=jack2008;";
//DbProviderFactory factory = DbProviderFactories.GetFactory("");
//using (DbConnection conn = factory.CreateConnection())
//{
// conn.ConnectionString = connString;
// TestConnection(conn);
//}
Console.WriteLine("go");
Console.ReadLine();
}
private static void ShowAllDbProviderFactories()
{
/* all Db Provider Factory */
DataTable dtFactory = DbProviderFactories.GetFactoryClasses();
Console.WriteLine("------------------------------------");
foreach (DataRow row in dtFactory.Rows)
{
//for (int i = 0; i < dtFactory.Columns.Count; i++)
//{
// Console.Write("{0}\t", row[i]);
//}
Console.Write("{0}\t{1}", row[2], row[3]);
Console.WriteLine();
}
}
private static void UseDbProviderFactoryGetConnection(string providerInvariantName, string connString)
{
DbProviderFactory factory = DbProviderFactories.GetFactory(providerInvariantName);
using (DbConnection conn = factory.CreateConnection())
{
conn.ConnectionString = connString;
TestConnection(conn);
}
}
private static void TestConnection(DbConnection conn)
{
Console.WriteLine("------------------------------------");
Oracle 宣布 其 NoSQL 数据库 2.0 版本,该数据库在 2011年11月首次发布。此新版本据说更好的集成 Oracle 数据库和 Hadoop 环境。基于高可用性版本的 Berkley DB key/value 存储。该系统基于 GPL 兼容的 Sleepycat licence (该许可是 OSI 认可的)。 Oracle NoSQL Database 2.0 可通过这里 下载 ,包括 AGPL 社区版和商业企业版。Oracle 的 NoSQL 数据库是 Oracle 大数据平台的一个关键组件。
据说 2.0 版本提供更好的性能,在使用 15 台 Oracle 企业 Linux 服务器、两个 335GB 的 SSD 卡和 2.4TB 数据库下获得了 100万雅虎云服务基准测试操作(详情)。新版本同时提供了自动重新均衡;新的 Web 控制台支持 SNMP/JMX;新的 C API 支持大对象;可直接在 Oracle 数据库中通过 SQL 语句查询 NoSQL 数据库的信息。
本文链接
在Red Hat Enterprise Linux Server Releae 5.5 成功安装ORACLE 10g 后,在客户端配置TNS后,测试是否可以连接到数据块服务器,结果报错: ORA-12170:TNS:连接超时
1:首先检查网络是否能ping通,如下所示,网络是畅通的。
2:检查TNS配置(TNS配置也没有问题)
GSP =
(DESCRIPTION =
(ADDRESS =(PROTOCOL = TCP)(HOST = 172.20.36.79)(PORT = 1521))
(CONNECT_DATA=
(SERVER = DEDICATED)
(SERVICE_NAME = gsp)
)
)
3:查看服务器监听服务是否启动
[oracle@wgods ~]$ lsnrctl status
LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 14-DEC-2012 15:51:13
Copyright (c) 1991, 2005, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date 14-DEC-2012 13:15:28
Uptime 0 days 2 hr. 35 min. 45 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /database/oracle/product/dbhome/network/admin/listener.ora
Listener Log File /database/oracle/product/dbhome/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=wgods)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "gsp" has 2 instance(s).
Instance "gsp", status UNKNOWN, has 1 handler(s) for this service...
Instance "gsp", status READY, has 1 handler(s) for this service...
Service "gspXDB" has 1 instance(s).
Instance "gsp", status READY, has 1 handler(s) for this service...
Service "gsp_XPT" has 1 instance(s).
Instance "gsp", status READY, has 1 handler(s) for this service...
The command completed successfully
4:使用tnsping命令检查,报TNS-12535: TNS: 操作超时,这时我们可以肯定是防火墙的问题了。
C:\Users\kerry>tnsping 172.20.32.79
TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 14-12月-2012 15:47:15
Copyright (c) 1997, 2010, Oracle. All rights reserved.
已使用的参数文件:
E:\app\kerry\product\11.2.0\dbhome_1\network\admin\sqlnet.ora
已使用 EZCONNECT 适配器来解析别名
尝试连接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST=172.20.32.79)(PORT=1521)))
TNS-12535: TNS: 操作超时
对于防火墙问题,我们可以有两种解决方案:
1:关闭防火墙(这种方案不怎么好,关闭防火墙,会带来许多安全隐患)
[root@wgods ~]# service iptables stop
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
2:修改iptables,开放1521端口,允许1521端口被连接
2.1 编辑iptables文件,添加-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 1521 -j ACCEPT记录。
[root@wgods sysconfig]# vi iptables
# Generated by iptables-save v1.3.5 on Fri Dec 14 17:03:58 2012
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1749:243629]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p esp -j ACCEPT
-A RH-Firewall-1-INPUT -p ah -j ACCEPT
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 23 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 1521 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri Dec 14 17:03