当前位置: 移动技术网 > IT编程>数据库>Oracle > C#利用ODP.net连接Oracle数据库的操作方法

C#利用ODP.net连接Oracle数据库的操作方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

630新闻直播,踏青的作文,皇朝秘史txt下载

本文介绍了c#连接oracle数据库的过程。通过instant client和odp.net中的oracle.dataaccess.dll,我们就可以方便的部署.net应用程序或者站点,而不需要安装oracle客户端。接下来我们就介绍这一过程。

1. odac的安装
在oracle的官方网站上下载与你安装的oracle对应版本的odac。
下载地址:odac download
下载好后解压安装,安装时不用安装全部的组件。主要安装以下组件:
oracle instant client
oracle data provider for .net2.0
oracle rovider for asp .net

2. 环境变量的设置
设置windows的环境变量:
oracle_home :odac的安装目录(类似 ~\app\administrator\product\11.1.0\client_1);
ld_library_path :%oracle_home%;
tns_admin : %oracle_home%;
在path的最前面追加:%oracle_home%;

3. 监听文件tnsnames.ora的配置
在目录%oracle_home%下新建文件tnsnames.ora,内容如下:
复制代码 代码如下:

数据库sid =
(description =
(address_list =
(address = (protocol = tcp)(host = oracle主机名或者ip)(port = 1521))
)
(connect_data =
(service_name = 数据库sid)
)
)

4. plsqldev
这样配置好后,plsqldev就可以连接上oracle数据库了。

5. c#连接oracle
c#连接oracle的示例代码如下:
复制代码 代码如下:

oracleconnection conn =
new oracleconnection();
try
{
conn.connectionstring = configurationmanager.connectionstrings["oradb"].connectionstring;
conn.open();
string sql = " select id,content from test"; // c#
oraclecommand cmd = new oraclecommand(sql, conn);
cmd.commandtype = commandtype.text;
oracledatareader dr = cmd.executereader(); // c#
list<string> contents = newlist<string>();
while(dr.read())
{
contents.add(dr["content"].tostring());
}
listbox1.itemssource = contents;
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
conn.clone();
}

在程序app.config或者web.config中追加数据库连接的配置。
复制代码 代码如下:

<connectionstrings >
<add name="oradb"connectionstring="data source=(description=
(address_list=(address=(protocol=tcp)(host=192.168.1.1)(port=1521)))
(connect_data=(service_name=****)));
user id=***;password=***;"/>
</connectionstrings>

按照上述的步骤执行,如果没有错误,就可以成功地连接数据库了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网