asp.net 连接 oracle10g 数据库

 

首先,进入 oracle 管理

asp.net 连接 oracle10g 数据库_第1张图片

 

找到 TNS 描述符,然后,复制下来。

 

进入 asp.net 项目,打开 web.config

 

 

<?xml version= " 1.0 " encoding= " utf-8 "?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http: // go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <appSettings>
    <add key= " AccessConnString " value= " provider=microsoft.jet.oledb.4.0;data source= "/>
    <add key= " AccessDbPath " value= " ~/App_Data/guestbook.mdb "/>
  </appSettings>
  <connectionStrings>
    <add name= " AccessConnectionString " connectionString= " Provider=Microsoft.Jet.Oledb.4.0;data source= "/>
    <add name= " Access_Path " connectionString= " ~/App_Data/guestbook.mdb "/>
    <add name= " SqlConnectionString " connectionString= " Data Source=localhost;Initial Catalog=HuaRunDb;User ID=sa;password=123456; " providerName= " System.Data.SqlClient "/>
    <add name= " OracleConnectionString " connectionString= " Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xp2008)(PORT=1521)))(CONNECT_DATA=(SID=guandb)(SERVER=DEDICATED)));User ID=system;Password=k2763826;Unicode=True " providerName= " System.Data.OracleClient "/>
  </connectionStrings>
  <system.web>
        <compilation debug= " true " targetFramework= " 4.0 " />
    </system.web>

</configuration>

 

 

填 写 OracleConnectionString

 

(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xp2008)(PORT=1521)))(CONNECT_DATA=(SID=guandb)(SERVER=DEDICATED)))

 

这一段就是 oracle10g 的 TNS 描述,然后,在后面写上登录用户名,密码, User ID=system;Password=k2763826;Unicode=True  就可以了。

 

后台程序

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
using System.Data.OracleClient;


public  void BindOracle()
        {

             string connstr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[ " OracleConnectionString "].ConnectionString;


            OracleConnection oraConn =  new OracleConnection(connstr);

             try
            {
                oraConn.Open();
                 string sql =  " select * from GUANUSER1.GUESTBOOK ";
                OracleDataAdapter myadapter =  new OracleDataAdapter(sql, oraConn);
                DataSet ds =  new DataSet();
                myadapter.Fill(ds);
                 this.GridView1.DataSource = ds;
                 this.GridView1.DataBind();
                 this.Label1.Text =  " 数据库连接成功! ";
            }
             catch (Exception ee)
            {
                 this.Label1.Text = ee.ToString();
            }

        }

 

注意 sql 写法,

string sql = "select * from GUANUSER1.GUESTBOOK";    select * from 用户.表
 

你可能感兴趣的:(oracle10g)