訪問して頂きありがとうございます。まさふくろーです。
前回、型付きデータセットを作成する方法をご紹介しましたが、このデータセットを使って画面に表示するプログラムのサンプルをご紹介します。
動作イメージ

画面イメージ

プログラムサンプル
Form1.vb
使用するクラスをインポート
| 
					 1  | 
						Imports System.Data.SqlClient  | 
					
Form1クラス内で使用する変数を宣言
| 
					 1 2 3 4  | 
						Public Class Form1      Dim conn As SqlConnection     Dim dtCustomers As DataSet1.DataTableSampleDataTable End Class  | 
					
SQLServerへ接続するメソッドを作成
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  | 
						Public Class Form1     ''' <summary>     ''' データベース接続     ''' </summary>     ''' <returns>接続成功:True、接続失敗:False</returns>     Private Function Connect() As Boolean         Dim result As Boolean = True         Try             conn = New SqlClient.SqlConnection             Dim serverName As String = "TEST-PC\SQLEXPRESS"             Dim dataBase As String = "Northwind"             'コネクションオープン             conn.ConnectionString =                 "Data Source = " & serverName &                 ";Initial Catalog = " & dataBase &                 ";Integrated Security = SSPI"             conn.Open()         Catch ex As Exception             result = False             System.Console.WriteLine(ex.Message)             Throw         End Try         Return result     End Function End Class  | 
					
Customersテーブルからデータ抽出、データテーブルにセットするメソッドを作成
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56  | 
						Public Class Form1     ''' <summary>     '''Customersテーブルを検索      ''' </summary>     ''' <param name="CustomerId"></param>     ''' <param name="CompanyName"></param>     ''' <returns></returns>     Private Function SerchCustomersData(ByVal CustomerId As String _                                   , ByVal CompanyName As String) As DataSet1.DataTableSampleDataTable         Dim dt As DataSet1.DataTableSampleDataTable         Using adapter As New SqlDataAdapter             Try                 Dim sql As String                 'データの取得                 sql = "SELECT "                 sql = sql & "* "                 sql = sql & "FROM Customers "                 sql = sql & "WHERE (CustomerID =@CustomerID OR @CustomerID = '') "                 sql = sql & "AND (CompanyName LIKE '%' + @CompanyName + '%'  OR @CompanyName = '') "                 sql = sql & "ORDER BY CustomerID "                 With adapter                     .SelectCommand = New SqlCommand(sql, conn)                     .SelectCommand.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerId                     .SelectCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = CompanyName                     .SelectCommand.CommandType = CommandType.Text                     Dim ds As New DataSet1                     .Fill(ds, "DataTableSample")                     dt = ds.Tables("DataTableSample")                 End With             Catch ex As SqlException                 System.Console.WriteLine(ex.Message)                 Throw             Catch ex As Exception                 System.Console.WriteLine(ex.Message)                 Throw             End Try         End Using         Return dt     End Function End Class  | 
					
SQLServerを切断するメソッドを作成
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  | 
						Public Class Form1     ''' <summary>     ''' データベース切断     ''' </summary>     ''' <returns>切断成功:True、切断失敗:False</returns>     Private Function DisConnect() As Boolean         Dim result As Boolean = True         Try             '既に開放されている場合はそのまま終わる             If conn Is Nothing Then                 Exit Try             End If             conn.Close()             conn.Dispose()             conn = Nothing         Catch ex As Exception             result = False             System.Console.WriteLine(ex.Message)             Throw         End Try         Return result     End Function End Class  | 
					
データ抽出ボタンのクリックイベントを作成
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34  | 
						Public Class Form1     ''' <summary>     '''データ抽出ボタンを押されたときの処理     ''' </summary>     ''' <param name="sender"></param>     ''' <param name="e"></param>     Private Sub btnDbSearch_Click(sender As Object, e As EventArgs) Handles btnDbSearch.Click         Try             '待機状態             Me.Cursor = Cursors.WaitCursor             'データベース接続             If Connect() = False Then Exit Sub             'SQLServerのテーブルを検索し、DataTableへセット             dtCustomers = SerchCustomersData(String.Empty, String.Empty)             'データベース切断             If DisConnect() = False Then Exit Sub             MessageBox.Show("データ取得完了!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)         Catch ex As Exception             MessageBox.Show(ex.Message)         Finally             '元に戻す             Me.Cursor = Cursors.Default         End Try     End Sub End Class  | 
					
一覧に表示ボタンのクリックイベントを作成
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29  | 
						Public Class Form1     ''' <summary>     ''' 一覧に表示ボタンを押されたときの処理     ''' </summary>     ''' <param name="sender"></param>     ''' <param name="e"></param>     Private Sub btnDtDisplay_Click(sender As Object, e As EventArgs) Handles btnDtDisplay.Click         Try             '待機状態             Me.Cursor = Cursors.WaitCursor             'DataTableをデータグリッドビューにセット             DataGridView1.DataSource = dtCustomers             MessageBox.Show("表示完了!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)         Catch ex As Exception             MessageBox.Show(ex.Message)         Finally             '元に戻す             Me.Cursor = Cursors.Default         End Try     End Sub End Class  | 
					
最後まで読んでいただき、ありがとうございました!
  本のまとめ関連記事はこちら

