C#连接Oracle 11g 无需安装Oracle客户端
|
admin
2018年7月17日 18:4
本文热度 5510
|
1.首先到Oracle网站上下载ODAC
下载地址:
附件:ODP.NET_Managed_ODAC12cR4.zip
2.下载的文件解压后找到Oracle.ManagedDataAccess.dll
3.将DLL文件拷贝到你的项目目录下,并引用进来即可
4.测试代码如下所示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.ManagedDataAccess.Client; //加入引用
using Oracle.ManagedDataAccess.Types; //加入引用
namespace ConnOracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ConnOracle()
{
try
{
string connStr = "User Id=userid;Password=userpwd;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=myDB)))";
using (var conn = new OracleConnection(connStr))
{
conn.Open();
DataSet ds = new DataSet();
string sql = "select * from user.testtable";
OracleDataAdapter oda = new OracleDataAdapter(sql, conn);
oda.Fill(ds);
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
}
catch (OracleException ex)
{
throw new Exception(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
ConnOracle();
}
}
}
该文章在 2018/7/17 18:04:41 编辑过