アプリケーションデータベースにデータを挿入する
以下のパターンに従って、RuntimePublic.Db APIを使用してアプリケーションデータベースにデータを挿入します。
DatabaseAccess
インスタンスからDatabaseProvider
を取得します。- プロバイダを使用してトランザクションを作成します。
- トランザクションを使用して
Command
を作成します。 Command
を使用してコマンドパラメータを作成します。Command
を実行します。Command
の結果を取得します。
例
以下の例は、アプリケーションデータベース内にあるUSEDITEM
テーブルにレコードを追加する方法を示しています。
C# コードの例
using OutSystems.RuntimePublic.Db; // [...] // Retrieve the DatabaseProvider for the current application DatabaseProvider dbaProvider = DatabaseAccess.ForRunningApplication(); // We will use this later to handle query parameters and escape identifiers SqlHelper sqlHelper = dbaProvider.SqlHelper; // Here we use the database transaction that is used to handle the current HTTP request // You can also use a separate transaction using (RequestTransaction requestTransaction = dbaProvider.GetRequestTransaction()) { using (Command cmd = requestTransaction.CreateCommand( string.Format("INSERT INTO USEDITEM ({0}, age) VALUES ({1}, {2})", // DESC is a keyword in SQL Server you need to escape it before using it as an // identifier sqlHelper.EscapeIdentifier("DESC"), // Parameter names need to be prefixed so they can be properly replaced by // their respective values.Each engine has its own parameter prefix so you can // use this function to make your SQL database agnostic sqlHelper.PrefixParam("desc"), sqlHelper.PrefixParam("age")))) { cmd.CreateParameter(sqlHelper.PrefixParam("desc"), DbType.String, "Car"); cmd.CreateParameter(sqlHelper.PrefixParam("age"), DbType.Int32, 3); cmd.ExecuteNonQuery(); } }