C#連線Oracle 11g 無需安裝Oracle客戶端
1.首先到Oracle網站上下載ODAC
下載地址1:http://download.csdn.net/detail/easyboot/9456476
下載地址2:http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html
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();
}
}
}