利用 System.Data.OracleClient.Dll 的组件进行连接:
首先配置WebConfig 文件;
然后添加System.Data.OracleClient.Dll 的引用才可以在页面中写代码using System.Data.OracleClient;
System.Data.OracleClient.Dll 常用的类:
System.Data.OracleClient.OracleConnection;
System.Data.OracleClient.OracleConnectionStringBuilder;
System.Data.OracleClient.OracleCommand;
System.Data.OracleClient.OracleCommandBuilder;
System.Data.OracleClient.OracleDataAdapter;
System.Data.OracleClient.OracleDataReader;
System.Data.OracleClient.OracleTransaction;
System.Data.OracleClient.OraclePerameter;
System.Data.OracleClient.OracleString;
System.Data.OracleClient.OracleType;
//前台显示
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
//后台代码实现:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient; //要引用的类库
public partial class _Default : System.Web.UI.Page
{
public System.Data.OracleClient.OracleConnection objConn; //声明一个OracleConnection对象
public System.Data.OracleClient.OracleCommand objCmd; //声明一个OracleCommand对象
public System.Data.OracleClient.OracleDataAdapter objAdp; //数据库适配器
public System.Data.DataSet dset; //DataSet 用于数据填充
//页面加载
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["oracleconn"].ConnectionString;
objConn = new OracleConnection(strconn);
}
//查询按钮设计
protected void Btn_Search_Click(object sender, EventArgs e)
{
try
{
string StrSql = "select * from pub_address";
objConn.Open();
objCmd = new OracleCommand(StrSql, objConn);
objAdp = new OracleDataAdapter(objCmd);
dset = new DataSet();
objAdp.Fill(dset);
objConn.Close();
GridView1.DataSource = dset;
GridView1.DataBind();
}
catch(Exception ex)
{
Label1.Text = ex.ToString();
return;
}
}
}
//以上简单实现从Oracle 中查询数据显示GridView 中, 当然重要的是数据库如何连接的部分。