您可以使用 SqlDataSource 控制項連接至 Oracle 資料庫,只要將連接字串資訊儲存在 Web 應用程式的 Web.config 檔,然後從 SqlDataSource 控制項參考連接字串 (Connection String) 即可。
N:System.Data.OracleClient Managed 提供者需要執行 ASP.NET 網頁的電腦上已安裝 Oracle 用戶端軟體 8.1.7 版 (含) 以上版本。
開啟 ASP.NET 應用程式根目錄中的 Web.config 檔。如果沒有 Web.config 檔案,請建立一個。
在 Configuration 項目中,如果 ConnectionStrings 項目不存在,請加入一個。
建立 add 項目做為 ConnectionStrings 項目的子系,定義下列屬性:
name 將值設定為您要用來參考連接字串 (Connection String) 的名稱。
connectionString 指派連接至 Oracle 資料庫所需的連接字串 (Connection String)。如需必須使用哪些連接字串值的詳細資訊,請聯絡資料庫管理員。
providerName 指派值 "System.Data.OracleClient",此值會指定 ASP.NET 在使用此連接字串進行連接時,應該使用 ADO.NET System.Data.OracleClient 提供者。
您完成的 add 項目可能如下:
<add name="OracleConnectionString" connectionString="Data Source=OracleServer1;Persist Security Info=True;Password="******";User ID=User1" providerName="System.Data.OracleClient" />
如果您在連接字串 (Connection String) 中包含明確的驗證資訊,則應該加密 Web.config 檔的 ConnectionStrings 區段。如需詳細資訊,請參閱使用受保護的組態加密組態資訊。
儲存 Web.config 檔。
在您要連接至 Oracle 資料庫的頁面中,加入 SqlDataSource 控制項。
在 SqlDataSource 控制項中,指定下列屬性 (Property):
SelectCommand 設定為用來擷取資料的 SQL select 陳述式,如下列範例所示:
SelectCommand="select * from customers"
ConnectionString 設定為您在 Web.config 檔案中建立的連接字串 (Connection String) 名稱。
ProviderName 設定為您在 Web.config 檔中指定的提供者名稱。
您也可以定義 UpdateCommand、DeleteCommand 和 InsertCommand 屬性 (Property)。如需詳細資訊,請參閱 SqlDataSource 控制項。
SqlDataSource 控制項宣告類似如下:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="select * from products" ConnectionString="<%$ ConnectionStrings:OracleConnectionString%>" ProviderName="<%$ ConnectionStrings:AppConnectionString1.ProviderName %>" />