外部データベースのデータにクエリを実行する
以下のパターンに従って、RuntimePublic APIを使用して外部データベースのデータにクエリを実行します。
DatabaseAccess
インスタンスからDatabaseProvider
を取得します。- プロバイダを使用してトランザクションを作成します。
- トランザクションを使用して
Command
を作成します。 Command
を使用してコマンドパラメータを作成します。Command
を実行します。Command
の結果を取得します。
例
この例では、環境管理コンソールで定義された「Oracle DB Connection」という接続を使用してアクセスする外部データベースがあります。
外部OracleデータベースのPERSONテーブルから20歳未満の人のレコードを取得します。
C# コードの例
using OutSystems.RuntimePublic.Db; // [...] // Retrieve the DatabaseProvider for the external database. // You need to have a Database Connection called “Oracle DB Connection” configured // in Service Center. DatabaseProvider dbaProvider = DatabaseAccess.ForExternalDatabase("Oracle DB Connection"); // We will use a separate transaction to execute the query // A committable transaction can be committed and rolled back explicitly using (CommittableTransaction commitableTransaction = dbaProvider.GetCommittableTransaction()) { using (Command cmd = commitableTransaction.CreateCommand("SELECT AGE FROM PERSON WHERE AGE < 20")) { // Results are read using a standard IDataReader object using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader.GetInt32(0)); } } } }