1.首先到Oracle網(wǎng)站上下載ODAC
下載地址:
附件:ODP.NET_Managed_ODAC12cR4.zip
2.下載的文件解壓后找到Oracle.ManagedDataAccess.dll
3.將DLL文件拷貝到你的項(xiàng)目目錄下,并引用進(jìn)來(lái)即可
4.測(cè)試代碼如下所示
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 編輯過