2018年4月25日水曜日

Accessアクセス from VB ~Class化してみるよ~(4)

RecordCountとColumnCountをRead Only Propeatyとして参照できるようにしてみた。
Class内部にTableもつのなら、Open、Closeにして、メモリ解放できるようにしたほうがいい気がしてきた…。
そろそろマジメにインターフェイスを考えよう。

Public Class SetDBtoTable
    '----------
    'Variable
    '----------
    Private _Provider As String
    Private _DataSource As String
    Private _ConnectionString As String
    Private _UserID As String
    Private _Password As String
    Private _RecordCount As Long
    Private _ColumnCount As Long
    '    Private _DataTable[][] As String  'テーブルデータ格納場所
    '----------
    'Property
    '----------
    Public Property Provider As String
        Get
            Return _Provider
        End Get
        Set(value As String)
            _Provider = value
        End Set
    End Property
    Public Property DataSource As String
        Get
            Return _DataSource
        End Get
        Set(value As String)
            _DataSource = value
        End Set
    End Property
    Public Property ConnectionString As String
        Get
            Return _ConnectionString
        End Get
        Set(value As String)
            _ConnectionString = value
        End Set
    End Property
    Public Property UserID As String
        Get
            Return _UserID
        End Get
        Set(value As String)
            _UserID = value
        End Set
    End Property
    Public Property Password As String
        Get
            Return _Password
        End Get
        Set(value As String)
            _Password = value
        End Set
    End Property
    Public ReadOnly Property RecordCount As Long
        Get
            Return _RecordCount
        End Get
    End Property
    Public ReadOnly Property ColumnCount As Long
        Get
            Return _ColumnCount
        End Get
    End Property
    '----------
    'Constructor
    '----------
    Public Sub New()
        Debug.Print("Constructor:" & TypeName(Me))
        _Provider = "Microsoft.Jet.OLEDB.4.0;"
    End Sub
    '----------
    'Destructor
    '----------
    Private Sub Class_Terminate()
        Debug.Print("Destructor:" & TypeName(Me))
    End Sub
    '----------
    'Method
    '----------
    Public Function GetTable() As Boolean
        Dim OnOK As Boolean = True
        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        cn = New ADODB.Connection
        rs = New ADODB.Recordset
        cn.Provider = _Provider
        '_ConnectionStringが空か確認。空であればエラーを吐きFalseを返してMethod終了
        If _ConnectionString = Nothing Then
            Debug.Print("ERROR:" & TypeName(Me) & ":ConnectionString Property Not Assignment”)
            OnOK = False
            Return OnOK
        End If
        'DataSourceが空か確認。空であればエラーを吐きFalseを返してMethod終了
        If _DataSource = vbNullString Then
            Debug.Print("ERROR:" & TypeName(Me) & ":DataSource Property Not Assignment”)
            OnOK = False
            Return OnOK
        End If
        'DataSource設定(テーブ名やSQL。DELETEとか書かれても、rs.open()のときにReadOnlyで開くからはじけると思う
        cn.Properties("Data Source").Value = _DataSource
        'ID/Passwd設定(試してない
        If _UserID <> Nothing Then
            cn.Properties("UserID").Value = _UserID
        End If
        If _Password <> Nothing Then
            cn.Properties("Password").Value = _Password
        End If
        Try
            cn.Open()
            rs.Open(_ConnectionString, cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
            _RecordCount = rs.RecordCount
            _ColumnCount = rs.Fields.Count
            rs.Close()
            cn.Close()
        Catch ex As Exception
            'Try中のエラーのとき
            Debug.Print("ERROR:" & TypeName(Me) & ":DB or RS Open failed”)
            OnOK = False
        End Try
        Return OnOK
    End Function
End Class



<<前  次>>




0 件のコメント:

コメントを投稿