Oracle Data Access Components (ODAC)这个组件可以让你很的方便连接Oracle,不用配置TNS文件,也不用安装客户端.不过用了之后再也没有思考过各个部分是64位还是32位的问题了.我安装的是Oracle 12C,官网下载对应的版本:http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html
安装后,E:\app\wendi1\product\12.1.0\dbhome_1\ODP.NET\managed\common目录中存在Oracle.ManagedDataAccess.dll文件,引用即可.下面是简单的demo.
aspx.cs文件:
using System; using Oracle.ManagedDataAccess.Client; public partial class ConnOracle: System.Web.UI.Page { public String SName { get; set; } public int Result1 { get; set;} public int Result2 { get; set; } public int Result3 { get; set; } protected void Page_Load(object sender,EventArgs e) { this.SName = "你好呀 ! "; int id = 4; string tableName = "haode"; OracleConnection conn = new OracleConnection("User ID=C##wendi;Password=0000;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))"); conn.Open(); //查 //表名没用到占位符,好像没这个需求 OracleCommand cmd = new OracleCommand("select name from haode where id=:id",conn); //OracleParameter para1 = new OracleParameter(":haode",tableName); //cmd.Parameters.Add(para1); OracleParameter para2 = new OracleParameter(":id",id); cmd.Parameters.Add(para2); OracleDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { this.SName = dr.GetOracleString(0).ToString(); } cmd.Dispose(); dr.Close(); //增 //这里可以不用commit,但是cmd中一定. OracleCommand cmd1 = new OracleCommand("insert into haode values(5,'文迪','haha5')",conn); this.Result1 += cmd1.ExecuteNonQuery(); this.Result1 += cmd1.ExecuteNonQuery(); cmd1.Dispose(); //删 OracleCommand cmd2 = new OracleCommand("delete from haode where id=5",conn); this.Result2 = cmd2.ExecuteNonQuery(); cmd2.Dispose(); //删 OracleCommand cmd3 = new OracleCommand("update haode set character='改了' where id=10",conn); this.Result3 = cmd3.ExecuteNonQuery(); cmd3.Dispose(); conn.Close(); } }
aspx:
<div> <h1> <%= this.SName %> </h1> <h2>增结果 : <%=this.Result1 %></h2> <h2>删结果 : <%=this.Result2 %></h2> <h2>改结果 : <%=this.Result3 %></h2> </div>