C# 多种方式连接Oracle。
废话不多说直接正题:
首先我们先在Oracle数据库下建了一个用户叫做lisi,密码为lisi,在这个用户下建立一张表叫做“USERS”,在这个表下新增三个数据。

方式一:利用OleDb连接Oracle。
#region 使用OleDb方式
Console.WriteLine("OleDb方式");
OleDbConnection oledbConnection = new OleDbConnection("Provider=OraOLEDB.Oracle; Data Source=MLDN; User Id=lisi; Password=lisi;");
OleDbCommand oledbCommand = new OleDbCommand();
oledbCommand.CommandText = "select * from USERS";
oledbCommand.CommandType = CommandType.Text;
oledbCommand.Connection = oledbConnection;
oledbConnection.Open();
var oledbReader = oledbCommand.ExecuteReader();
while (oledbReader.Read())
{
Console.WriteLine("姓名:"+oledbReader[]);
}
oledbConnection.Close();
#endregion
方式二:利用Oracle.ManagedDataAccess.Client方式,在使用前,我们需要先在Nuget中找到Oracle.ManagedDataAccess,然后安装,该方式好处之一是不需要在客户端上安装Oracle客户端,该版本是由Oracle公司开发的,而且微软已经放弃了自己的System.Data.OracleClient方式(下面会有介绍),推荐使用这种方式。

程式码如下:
#region OracleConnection
//OracleConnection connection = new OracleConnection("Password=lisi;User ID=lisi;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.229.138)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=****)))");没有安装客户端的话,可以使用这种方式
Console.WriteLine("Oracle.ManagedDataAccess.Client方式");
OracleConnection oralceConnection = new OracleConnection("Data Source=MLDN; User Id=lisi; Password=lisi;");
oralceConnection.Open();
OracleCommand oracleCommand = new OracleCommand();
oracleCommand.CommandText = "select * from USERS";
oracleCommand.CommandType = CommandType.Text;
oracleCommand.Connection = oralceConnection;
var oracleReader = oracleCommand.ExecuteReader();
while (oracleReader.Read())
{
Console.WriteLine("姓名:" + oracleReader[]);
}
oralceConnection.Close();
#endregion
方式三:利用odbc方式
#region odbc
Console.WriteLine("odbc方式");
OdbcConnection odbcConnection = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=MLDN;Uid=lisi;Pwd=lisi;");
odbcConnection.Open();
OdbcCommand odbcCommand = new OdbcCommand();
odbcCommand.CommandText = "select * from USERS";
odbcCommand.CommandType = CommandType.Text;
odbcCommand.Connection = odbcConnection;
var odbcReader= odbcCommand.ExecuteReader();
while (odbcReader.Read())
{
Console.WriteLine("姓名:" + odbcReader[]);
}
odbcConnection.Close();
#endregion
方式四:利用System.Data.OracleClient方式(该方式已过时,不推荐使用)。

虽然已经过时了但是还是可以使用的,代码如下:
#region
Console.WriteLine("System.Data.OracleClient方式");
System.Data.OracleClient.OracleConnection clientConnection = new System.Data.OracleClient.OracleConnection();
clientConnection.ConnectionString = " Data Source=MLDN; User Id=lisi; Password=lisi;";
System.Data.OracleClient.OracleCommand clientCommand = new System.Data.OracleClient.OracleCommand();
clientCommand.CommandText = "select * from USERS";
clientCommand.Connection = clientConnection;
clientCommand.CommandType = CommandType.Text;
clientConnection.Open();
var clientReader = clientCommand.ExecuteReader();
while (clientReader.Read())
{
Console.WriteLine("姓名:" + clientReader[]);
}
clientConnection.Close();
#endregion
方式五:使用可以连接Oracle的Entity Framework工具,在使用前需要先安装一个插件包,我的VS是2013的,插件包在如下地址:
http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/odacmsidownloadvs2013-2756823-zhs.html
当安装好了之后,我们可以使用EF去连接Oralce资料库了,连接方式如下:


然后我们在UI画面中拉取我们需要用到的表,注意使用EF的时候是要设置表主键的,所以我们需要设置USERS这张表的ID为主键,否则没有办法拉取到该表对象。

代码如下:
#region Oracle EF
Console.WriteLine("Oracle EF方式");
Entities db = new Entities();
var users = db.USERS.ToList();
foreach (var item in users)
{
Console.WriteLine("姓名:" + item.NAME);
}
#endregion
执行结果画面:

综上是我目前所知道的连接Oracle的方式,至于还有其他方式,我暂时可能就不太清楚了。
参考文章:
http://blog.csdn.net/zz155666/article/details/53163115
https://www.cnblogs.com/gguozhenqian/p/4262813.html
C# 多种方式连接Oracle。的更多相关文章
- Java笔记4:JDBC纯驱动方式连接Oracle
JDBC纯驱动方式连接Oracle 1 下载Oracle提供的驱动程序包 下载地址: http://www.oracle.com/technetwork/database/enterprise-edi ...
- java thin方式连接oracle数据库
本文主要描述通过thin方式连接oracle数据库 1.创建web project ,将D:\oracle\product\10.2.0\db_1\jdbc\lib(oracle安装目录)下的ojdb ...
- vc++用ADO方式连接oracle问题
今天装了个oracle客户端,准备写个访问远程oracle的程序.用的是vs2010,采用ADO的连接方法连接oracle,结果运行的时候总是报下面的错: 从提示可以看出是没有找到OraOLEDBup ...
- win7下VS.NET中通过LinqToSQL连接oracle数据库
.NetFramework3.5提供了LinqToSQL组件,为我们访问数据库提供了方便.我用的是VS+Oracle开发工具.也想体验一下快捷方便的感觉. 1.连接Oracle数据库 在连接Oracl ...
- C++ 连接Oracle
下面是一个ADO方式连接Oracle的小程序部分代码...... 首先是Oracle的配置.在Oracle的安装路径下找到:Oracle\network\ADMIN\tnsnames.ora文件.配置 ...
- atitit.client连接oracle数据库的方式总结
client连接oracle数据库的方式总结 文件夹 Java程序连接一般使用jar驱动连接.. ... 桌面GUI一般採取c语言驱动oci.dll 直接连接... 间接连接(须要配置tns及其env ...
- c# Oracle 远程连接方式 plsql 连接oracle 11g 64位
1.本地连接字符串: string connect = "Data Source=orcl;user=XXX;password=XXX;Persist Security Info=Tru ...
- 连接Oracle的几种方式
如何引用Data.OracleClient.dll 由于从.net 4.0之后,微软将OracleClient.dll从框架里去除了,所以要使用,需要在VS2010里面去把项目的.net框架从.net ...
- Oracle BEQ方式连接配置
Oracle BEQ方式连接配置 服务端和客户端在同一台机器上,可以使用BEQ连接,BEQ连接可以理解为进程间直接通信,不需要走网络监听,性能更高. 可以参考MOS:How To Connect Us ...
随机推荐
- 调用系统命令之subprocess模块
除了常见的os.system和os.popen方法,官方强烈推荐使用subprocess来调用系统命令. 这个库用起来其实很简单,按照惯例先贴一下官文关键点: The subprocess modul ...
- CF986C AND Graph
半年前做的一道题现在还是不会 x&y=0 意味着,x的补集的子集都是和x直接相连的 不妨令图中的点数就是2^n 那么可以直接从x^((1<<n)-1)开始记忆化爆搜,路上遇到的都是 ...
- windows下用bat启动jar包,修改cmd标题(title)
新建start.bat,输入以下内容即可. @echo off title myprogress-%date%-%time%-%cd% java -jar myprogress.jar
- java:取屏幕大小(去掉任务栏的高度部分)
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); Insets screenInsets = Toolkit.getD ...
- oi程序提交注意:bool
比如我一个程序用了bool类型(#include<stdbool.h>) 在poj以c的方式提交不通过显示Compile Error,而用gcc的方式提交通过了, 也许其它的#includ ...
- malloc创建三维数组
#include <stdio.h> #include <stdlib.h> #include <malloc.h> int main() { //f[0],f[] ...
- powerdesigner 字段添加注释和默认值
powerdesigner 字段添加注释和默认值 2017年01月06日 10:59:02 qingzhuoran 阅读数:27161更多 个人分类: powerdesigner 1.选中表,右键 ...
- 关于setvbuf()函数的详解
为什么要使用setvbuf函数 如果你的内存足够大,可以把文件IO的BUF设置大一些,这样每次你用fopen/fread/fwrite/fscanf/fprintf语句的时候,都会在内存里操作,减少内 ...
- springboot的起步依赖
加载自动配置的方式2: springboot读取配置文件的方式: 1.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或applicatio ...
- jsp中的request.getContextPath()
jsp中的request.getContextPath() <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但不用也可以吧,比 ...