首先,進入 oracle 管理
找到 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 使用者.表