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 { } } }
|
请发表评论