''' <summary>商品</summary> Public Class Product ''' <summary>商品コード</summary> Public Property ProductCode As String ''' <summary>商品名</summary> Public Property ProductName As String ''' <summary>現行品区分</summary> Public Property IsActive As Boolean End Class
Public Class ProductAccess ''' <summary>商品コードを指定して、該当する商品を取得します。</summary> Public Function GetProduct(productCode As String) As Model.Product Using command As New Microsoft.Data.SqlClient.SqlCommand command.CommandText = "SELECT * FROM T_PRODUCT WHERE productCode = @productCode" command.Parameters.Add("@productCode", Data.SqlDbType.NVarChar).Value = productCode Using table As Data.DataTable = DbUtil.Select(command) Dim result As New Model.Product result.ProductCode = CStr(table.Rows(0)("ProductCode")) result.ProductName = CStr(table.Rows(0)("ProductName")) result.IsActive = CBool(table.Rows(0)("IsActive")) Return result End Using End Using End Function ''' <summary>商品コードから商品を検索します。該当する商品の一覧を返します。</summary> ''' <param name="partOfproductCode">検索する商品コードの一部</param> Public Function FindProduct(partOfproductCode As String) As List(Of Model.Product) Using command As New Microsoft.Data.SqlClient.SqlCommand command.CommandText = "SELECT * FROM T_PRODUCT WHERE productCode LIKE CONCAT( N'%', @productCode, N'%' )" command.Parameters.Add("@productCode", Data.SqlDbType.NVarChar).Value = partOfproductCode Using table As Data.DataTable = DbUtil.Select(command) Dim results As New List(Of Model.Product) For Each row As Data.DataRow In table.Rows Dim result As New Model.Product result.ProductCode = CStr(row("ProductCode")) result.ProductName = CStr(row("ProductName")) result.IsActive = CBool(row("IsActive")) results.Add(result) Next Return results End Using End Using End Function End Class
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click Dim productAccess As New DataAccess.ProductAccess Dim results As List(Of Model.Product) = productAccess.FindProduct(txtProductCode.Text) MsgBox($"{results.Count} 件ヒットしました。") MsgBox($"1件目 {results(0).ProductName}") End Sub