Asp.Net 访问 Oracle 的简单例子
1.为灵活设置,将连接字符写在web.config文件中
<appSettings> <add key="oracleconn" value="User ID=terryfeng;Password=frt_2007;Data Source=test;"/>
<add key="oracleconn" value="User ID=terryfeng;Password=frt_2007;Data Source=(DESCRIPTION = (ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST=192.168.1.200)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=test.finemoon)))" />
</appSettings>
字符串说明:
需要定义Net服务: <add key="参数名" value="User ID=用户名;Password=用户密码;Data Source=数据源;"/>
注意:这种连接方法可能会出现权限问题请参考 我的另一篇随笔解决“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本。”(图)
无需定义Net服务: <add key="参数名" value="User ID=用户名;Password=用户密码;Data Source=(DESCRIPTION =
(ADDRESS_LIST = (ADDRESS = (PROTOCOL = 协议)(HOST=主机)(PORT=端口)))(CONNECT_DATA=(SERVICE_NAME=服务名)))" />
注意: 这种方法有一个问题是,无法想上面的服务那样一次定义服务,在服务下面再定义多个连接
2.为Web 应用程序 添加 System.Data.OracleClient 程序集引用
3.下面是测试的程序,
aspx 文件代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspNetOra._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <p> 调用一个Oracle连接并执行一条语句测试是否成功<asp:Label ID="Label1" runat="server" ForeColor="Red" Text=""></asp:Label> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="测试连接是否成功" /> </p> <p> 调用给SQL语句执行绑定<br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <br /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回数据集合,绑定Gridview" /> </p> <p> 输入输出参数的存储过程调用<br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button3" runat="server" Text="z输入输出参数的存储过程" OnClick="Button3_Click" /> </p> <p> 执行返回数据集的Oracle存储过程<br /> <asp:GridView ID="GridView2" runat="server"> </asp:GridView> <asp:Button ID="Button4" runat="server" Text="执行存储过程返回数据集" OnClick="Button4_Click" /> </p> </div> </form> </body> </html>
aspx.cs 文件代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data.OracleClient; namespace AspNetOra { public partial class _Default : System.Web.UI.Page { System.Data.OracleClient.OracleConnection objConn; //声明一个OracleConnection对象 System.Data.OracleClient.OracleCommand objCmd; //声明一个OracleCommand对象 string strcon = System.Configuration.ConfigurationManager.AppSettings["oracleconn"]; //从Web.config 文件里调用数据库连接字符串 protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 测试连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { try { objConn = new OracleConnection(strcon); //用连接字符串,实例化连接对象 objConn.Open(); //打开数据库连接 string strSQL = "select * from sys.test1"; //查询sys建立的test1表的内容 因为我现在用的登录用户不是sys了 objCmd = new OracleCommand(strSQL, objConn); //用命令和连接对象建立命令实例 objCmd.ExecuteNonQuery(); objConn.Close(); this.Label1.Text = "成功"; } catch { this.Label1.Text = "失败"; } } /// <summary> /// 执行SQL 语句返回数据集合 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { objConn = new OracleConnection(strcon); //用连接字符串,实例化连接对象 objConn.Open(); //打开数据库连接 System.Data.OracleClient.OracleDataAdapter adapter = new OracleDataAdapter("select * from sys.test1", objConn); System.Data.DataSet ds = new System.Data.DataSet(); adapter.Fill(ds); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } /// <summary> /// 存储过程输入、输出参数 /// /// 存储过程定义如下: /// /// create or replace procedure getName(name_out out varchar2 ,id_in in varchar2) is /// begin /// select NAME into name_out from test1 where id = id_in; /// end getName; /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { objConn = new OracleConnection(strcon); //用连接字符串,实例化连接对象 objConn.Open(); //打开数据库连接 OracleCommand cmd = new OracleCommand("getName", objConn); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("name_out", OracleType.VarChar, 20); //输出参数,注意名字要与存储过程一致 cmd.Parameters["name_out"].Direction = System.Data.ParameterDirection.Output; cmd.Parameters.Add("id_in", OracleType.VarChar, 20); //输入参数,注意名字要与存储过程一致 cmd.Parameters["id_in"].Direction = System.Data.ParameterDirection.Input; cmd.Parameters["id_in"].Value = "1"; //给输入参数赋值 cmd.ExecuteNonQuery(); this.TextBox1.Text = cmd.Parameters["name_out"].Value.ToString(); //获得输出参数 } /// <summary> /// Oracle存储过程并不直接返回记录集,记录集以游标的形式通过参数返回。一个包(Packages可以包含多个存储过程, /// 访问存储过程时采用“包名.存储过程名”的方式。 /// 存储过程是我上一个随笔写的Test,但是上一个存储过程是用Sys建立的,在这里我又重新用当前用户建立的一个属于这个用户的包 /// /// 包定义如下: /// 包头: /// create or replace package test is /// TYPE MYCURSOR IS REF CURSOR; /// PROCEDURE GETLIST(cur_OUT OUT MYCURSOR); /// end test; /// /// /// /// 包体: /// create or replace package body test is /// PROCEDURE GETLIST(cur_OUT OUT MYCURSOR) AS /// BEGIN /// OPEN cur_OUT FOR SELECT * FROM test1; /// END; /// end test; /// /// /// /// /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button4_Click(object sender, EventArgs e) { objConn = new OracleConnection(strcon); //用连接字符串,实例化连接对象 objConn.Open(); //打开数据库连接 OracleCommand cmd = new OracleCommand("test.GETLIST", objConn); cmd.Parameters.Add("cur_OUT", OracleType.Cursor); //注意这里的类型 cmd.Parameters["cur_OUT"].Direction = System.Data.ParameterDirection.Output; cmd.CommandType = System.Data.CommandType.StoredProcedure; OracleDataAdapter da = new OracleDataAdapter(cmd); System.Data.DataSet ds = new System.Data.DataSet(); da.Fill(ds); GridView2.DataSource = ds.Tables[0].DefaultView; GridView2.DataBind(); } } }
调用结果如下图:
程序下载:
http://zg6bbg.blu.livefilestore.com/y1p4XetCiqhCNLBrQSqDBJACDtbAhuJ-ov70gsIZT5TzE5OwW3dtacrwopdIrIZ8VQ2iK7M0rKsm36cR10sZHOKmA/AspNetOra.rar?download
参考更多技术博客了解Oracle返回不同结果的实现方法:
http://blog.csdn.net/web_gus/archive/2004/10/13/134907.aspx
http://www.cnblogs.com/volnet/archive/2007/01/22/627190.html
http://www.cnblogs.com/cwbboy/archive/2006/03/22/356017.html
转自:http://www.cnblogs.com/TerryFeng/archive/2009/03/08/1406366.html