1:首先在web配置文件对连接字符串进行配置
<connectionStrings>
<add name="conStr" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));User ID=admin1;Password=admin1"/> </connectionStrings>2:添加在项目里添加引用oracle的dll文件
3:在后台的命名空添加using System.Data.OracleClient;另外还得添加命名空间using System.Configuration;
using System;
using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.OracleClient;using System.Configuration;using System.Data;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowData();//调用自定义的函数 } } private void ShowData() { #region//主要练习用using语句连接oracle数据 string strCon = ConfigurationManager.ConnectionStrings["conStr"].ToString(); try { using (OracleConnection con = new OracleConnection(strCon)) { con.Open(); string sql = "select * from Student"; using (OracleCommand cmd = new OracleCommand(sql, con)) { using (OracleDataAdapter da = new OracleDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } catch (OracleException ex) { Response.Write("<script>alert('数据库出现异常')</script>"+ex.Message); } #endregion }}4:调试代码结果