Oracle的版本Oracle10g
1。將Oracle.DataAccess.dll加入引用,我使用的版本是2.112.1.2
2。將以下dll復制到運行目錄,例如debug目錄下,總之,要和項目最後的exe文件放在一個目錄下。
然後就可以了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.ClIEnt;//一定要用這個,不能用微軟的
private OracleConnection GetOracleConnection() string connstring =
"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.152)(PORT=1521))" +
"(CONNECT_DATA=(SID=orcl)));User Id=SmartDB;PassWord=zz;";//這個也可以放到Web.Config中。
OracleConnection conn = new OracleConnection(connstring);
return conn;
}
private void Oracle_ExecuteNonQuery(string sSql) OracleConnection conn = null;
try conn = GetOracleConnection();
conn.Open();
OracleCommand cmd = new OracleCommand(sSql, conn);
cmd.ExecuteNonQuery(); catch (Exception e) //MessageBox.Show(e.Message);
throw e; finally try conn.Close(); catch { } }
private DataTable Oracle_ExecuteQueryToDataTable(string sSql) OracleConnection conn = null;
DataTable dt = null;
try conn = GetOracleConnection();
conn.Open();
OracleCommand cmd = new OracleCommand(sSql, conn);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
return dt;
}
catch (Exception e) MessageBox.Show(e.Message);
throw e; finally try conn.Close(); catch { } }