ストアドプロシージャを呼び出す
ストアドプロシージャを呼び出すには、RuntimePublic.Db APIメソッドを呼び出して取得できる.NETネイティブオブジェクトをいくつか使用する必要があります。これらを取得するため、すべてのクエリオブジェクトには言語ネイティブオブジェクトを返すGetDriver<ClassName>
メソッドが含まれています。
.NETネイティブオブジェクトを使用する場合、以下に準拠する必要があります。
- データベースエンジンの固有の構文
- ドライバの固有の動作
例
この例では、環境管理コンソールで定義された「Data Warehouse」という接続を使用してアクセスする外部データベースがあります。
以下のコードは、従業員の業績評価を計算するCOMPUTE_SCORE
というストアドプロシージャを呼び出します。入力パラメータは従業員の名前で、整数のscore
出力パラメータは計算された従業員の業績評価に対応します。ストアドプロシージャによって業績評価が計算され、出力パラメータで返されます。
C# コードの例
using OutSystems.RuntimePublic.Db; // [...] // Get a reference (dbaProvider) to the external database "Data Warehouse" DatabaseProvider dbaProvider = DatabaseAccess.ForExternalDatabase("Data Warehouse"); SqlHelper sqlHelper = dbaProvider.SqlHelper; using (RequestTransaction trans = dbaProvider.GetRequestTransaction()) { using (Command cmd = trans.CreateCommand("COMPUTE_SCORE")) { // There is no API to set the command type so you need to access the // underlining ADO.NET objects cmd.GetDriverCommand().CommandType = CommandType.StoredProcedure; // Set the employee (input) and performanceScore (output) parameters cmd.CreateParameter("employee", DbType.String, "John Baker"); var performanceScore = cmd.CreateParameter("score", DbType.Int32, DBNull.Value); // Set parameter direction on ADO.NET Data Parameter // The driver will fill the output parameter with a value after the query executes performanceScore.GetDriverParameter().Direction = ParameterDirection.Output; // Call the store procedure cmd.ExecuteNonQuery(); // Print the store procedure's output Console.WriteLine(performanceScore.Value); } }