C#连接Oracle的方法实例总结
本文实例总结了c#连接oracle的方法。分享给大家供大家参考,具体如下:
一、通过system.data.oracleclient(需要安装oracle客户端并配置tnsnames.ora)
1. 添加命名空间system.data.oracleclient引用
2.
using system.data.oracleclient;
3.
string connstring = "user id=ifsapp;password=ifsapp;data source=race;"; oracleconnection conn = new oracleconnection(connstring); try { conn.open(); messagebox.show(conn.state.tostring()); } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
二、通过system.data.oracleclient(需要安装oracle客户端不需配置tnsnames.ora)
1. 添加命名空间system.data.oracleclient引用
2.
using system.data.oracleclient;
3.
string connstring = "user id=ifsapp;password=ifsapp;data source=(description = (address_list= (address = (protocol = tcp)(host = 127.0.0.1)(port = 1521))) (connect_data = (service_name = race)))"; oracleconnection conn = new oracleconnection(connstring); try { conn.open(); messagebox.show(conn.state.tostring()); } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
三、通过system.data.oledb和oracle公司的驱动
1. 添加命名空间system.data.oracleclient引用
2.
using system.data.oledb;
3.
string connstring = "provider=oraoledb.oracle.1;user id=ifsapp;password=ifsapp;data source=(description = (address_list= (address = (protocol = tcp)(host = 127.0.0.1)(port = 1521))) (connect_data = (service_name = race)))"; oledbconnection conn = new oledbconnection(connstring); try { conn.open(); messagebox.show(conn.state.tostring()); } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
四、通过system.data.oledb和微软公司的oracle驱动
1. 添加命名空间system.data.oracleclient引用
2.
using system.data.oledb;
3.
string connstring = "provider=msdaora.1;user id=ifsapp;password=ifsapp;data source=(description = (address_list= (address = (protocol = tcp)(host = 127.0.0.1)(port = 1521))) (connect_data = (service_name = race)))"; oledbconnection cnn = new oledbconnection(connstring); try { conn.open(); messagebox.show(conn.state.tostring()); } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
备注:
a.xp操作系统已经安装了微软公司的oracle驱动c:\program files\common files\system\ole db\msdaora.dll
b.该驱动需要oracle客户端的三个文件(oraocixe10.dll、oci.dll、ociw32.dll)放在system32下即可
五、使用odp连接
1. 下载安装odp.net(http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)
2. 安装完全成后会产生一序列文件。
3. 找到这个安装目录,打开文件夹%oracle_home%\network\admin在这个下面建立一个tnsnames.ora的文件,其内容可以参考其下的sample目录下面的配置
oracle.race = (description= (address_list= (address= (protocol=tcp) (host=127.0.0.1) (port=1521) ) ) (connect_data= (sid=race) (server=dedicated) ) )
oracle.race为连接字符串名称,可以随便取。等号后面的字符串可以在enterprise manager console工具中连接数据库后的tns描述符中拷过来
4. 引用oracle.dataaccess命名空间
5.
using oracle.dataaccess.client;
6. 示例代码:
string connstring = "data source=oracle.race;persist security info=true;user id=ifsapp;password=ifsapp"; oracleconnection conn = new oracleconnection(connstring); try { conn.open(); oraclecommand cmd = new oraclecommand(cmdtext,conn); oracledatareader reader = cmd.executereader(); this.datagridview1.datasource = reader; this.datagridview1.databind(); } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
六、使用第三方驱动
第三方驱动有 devart,下载驱动 http://www.devart.com/dotconnect/oracle/,但是是商业版,需要购买许可或破解
连接格式 user id=myusername;password=mypassword;host=ora;pooling=true;min pool size=0;max pool size=100;connection lifetime=0;
1. 引用devart.data.oracle命名空间
2.
using devart.data.oracle;
3.
oracleconnection conn = new oracleconnection(); conn.connectionstring = ""; conn.unicode = true; conn.userid = "ifsapp"; conn.password = "ifsapp"; conn.port = 1521; conn.server = "127.0.0.1"; conn.sid = "race"; try { conn.open(); //execute queries, etc } catch (exception ex) { showerrormessage(ex.message.tostring()); } finally { conn.close(); }
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。