« [VB.NET] クラス・構造体・モジュール | メイン | [VB.NET] DataTableの作成 »

2005年11月10日

[VB.NET] DataSet 一行追加・一行削除

        '--- 一行追加
        Dim DR As DataRow
        DR = DS.Tables("テーブル名").NewRow
        DR(0) = txtCol1  'DR(列名) でもOK
        DR(1) = txtCol2
        DR(2) = txtCol3
        DS.Tables("テーブル名").Rows.Add(DR)
        '--- 一行削除

'DataSetに主キーを設定
Dim pKey(0) As DataColumn
pKey(0) = DS.Tables("テーブル名").Columns("DBの列名")
DS.Tables("テーブル名").PrimaryKey = pKey

Dim DR As DataRow
DR = DS.Tables("テーブル名").Rows.Find(CType([文字列], Object)) 'DataSetの検索
If IsNothing(DR) = False Then
DS.Tables("テーブル名").Rows.Remove(DR) '一行削除
End If

2005 / 11 / 10