ADO NET Fill Data AdapterTable Adapter SQL Command
ADO. NET 非接続型の実装概念 Fill メソッド Data. Adapter/Table. Adapter オブジェクトにSQLクエリ(または ストアドプロシージャ)の Command を設定 ① コマンド設定 ② Fill メソッド実行 Data. Set Select. Command Insert. Command Update メソッド Data. Adapter Table. Adapter Update. Command Delete. Command Data. Base
ADO. NET 非接続型の実装概念 Update メソッド Data. Adapter/Table. Adapter オブジェクト の更新コマンドを設 定(更新コマンドの設定詳細は後述) Fill メソッド実行 Data. Set ②Update メソッド Select. Command Insert. Command Update. Command Data. Adapter Table. Adapter Delete. Command ① コマンド設定 Data. Base
ADO. NET 非接続型の実装概念 Row. State による状態管理 Fill 直後の Data. Table Row. State Column 1 Column 2 Unchanged 1 Dog Unchanged 2 Cat Unchanged 3 更新後の Data. Table 更 新 Snake Row. State Column 1 Column 2 Unchanged 1 Dog Modified 2 Lion Deleted 3 Snake Added 4 Cow 関連 Method • Cancel. Edit 直前の行の更新を キャンセル • Reject. Change 一連の更新処理を 元に戻す • Accept. Change 一連の更新処理を 確定する
ADO. NET その他の実装 パラメータの使用 Parameterオブジェクトを使用することで、単なる文字列置換でな い、意味的な引数管理をおこなう Command. Text のSQL文中で、パラメタ位置を指定 Sql. Client では @xxxx で指定、Ole. Db では ? で指定 Parameterオブジェクトを使ってパラメータをアタッチ Parameter object をAdd Value プロパティにパラメータの内容(入力引数)を設定 ‘SQL Server Client Dim cmd As New Sql. Command( “select * from authors where au_id = @au_id”, conn ) cmd. Parameters. Add(“@au_id”, Sql. Db. Type. Var. Char, 11) cmd. Parameters(“@au_id”). Value = “ 172 -32 -1176” : ‘ OLE DB Client Dim cmd As New Ole. Db. Command( “select * from authors where au_id = ? ”, conn ) cmd. Parameters. Add(“au_id”, Ole. Db. Type. Var. Char, 11 ) cmd. Parameters(0). Value = “ 172 -32 -1176” :
ADO. NET その他の実装 ストアドプロシージャの使用 非接続型においても、コマンドに “ロジック” を実装 できるため有用 パラメータのない簡単なストアドプロシージャの実装方法 Command. Text プロパティ、Command. Type プロパティを設定 Dim cmd As New Sql. Command(conn) cmd. Command. Text = “My. Procedure” cmd. Command. Type = Command. Type. Stored. Procedure conn. Open() Sql. Data. Reader dr = cmd. Execute. Reader() : パラメータを持つストアドプロシージャの実装方法 Direction プロパティ (Output / Input / Return. Value) でさまざまな引数を指定 Dim cmd As New Sql. Command(conn) Dim param As New Sql. Client. Sql. Parameter cmd. Command. Text = “My. Procedure” cmd. Command. Type = Command. Type. Stored. Procedure cmd. Parameters. Add(“@Param 1”, Sql. Db. Type. Int). Value = 3 param. Direction = Parameter. Direction. Return. Value param. Parameter. Name = “@Return. Value” param. Sql. Db. Type = Sql. Db. Type. Int cmd. Parameters. Add(param) : 入力パラメータの場合 返り値の場合
データアクセスにおける実装上の留意点 移植性への配慮(プロバイダ非依存な実装) 方法1: System. Data. Common 名前空間とDb. Provider. Factory を使用 (ADO. NET 2. 0の新機能) ‘ プロバイダ依存な記載 Dim data. Factory As Db. Provider. Factory = Db. Provider. Factories. Get. Factory("System. Data. Sql. Client") ‘ 以降、プロバイダ非依存な記載 Dim cmd As Db. Command = conn. Create. Command() cmd. Command. Text = "SELECT Name FROM Sales. Store WHERE Customer. ID = @Customer. ID" Dim param As Db. Parameter = data. Factory. Create. Parameter() param. Parameter. Name = "@Customer. ID" param. Value = txt. Customer. ID. Text cmd. Parameters. Add(param) Dim customer. Name As String = cmd. Execute. Scalar() 方法 2: インタフェースへのキャストを使用 ‘ プロバイダ依存な記載 IDb. Connection con = new Sql. Connection(“Server=(local); Integrated Security=SSPI; Database=northwind”); ‘ 以降、プロバイダ非依存な記載 string sql. Str = "SELECT id, name FROM Test. Table"; IDb. Command cmd = con. Create. Command(); cmd. Command. Text = sql. Str; con. Open(); IData. Reader dr = cmd. Execute. Reader();
トランザクションの管理 2. 0 で追加された System. Transaction を使用すると、 Transaction. Scope を使用したプログラミングモデルを使用可能 SQL Server 2005 では、状況に応じ、LTM から DTC (Ole. Tx) に自動 的に昇格 ネイティブ トランザクション System. Enterprise. Services (Ole. Tx , DTC) System. Transaction VS スケーラブルでない! u 分散環境に未対応 オーバーヘッド大 (遅い!) u マルチリソースに未対応 Using tx As New Transaction. Scope() Dim My. Con As Sql. Connection = New Sql. Connection("server=My. Server; " & _ "database=Adventure. Works; user id=My. User; password=My. Password") My. Con. Open() : Dim Rec. Num As Integer = My. Command. Execute. Non. Query() My. Con. Close() tx. Complete() End Using
技術情報リソース 「300 秒でズバリ !!」&「10 行でズバリ !!」シリーズ: http: //www. microsoft. com/japan/msdn/thisweek/300 x 10/ Step 7 シリーズ 「分散アプリケーション開発編」: http: //www. microsoft. com/japan/msdn/thisweek/step 7/defa ult. aspx Microsoft patterns & practices: http: //msdn. microsoft. com/practices/ マイクロソフトのコンサルタントが執筆! 『. NET エンタープライズWebアプリケーション 開発技術大全』(全 5巻)
© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U. S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
- Slides: 37