Description
Display data from database to DataGridView using ADODB.
HowTo
1. Add Refferences
Project>Refferences>Microsoft Active X Data Object 2.7 Library
or you can add manual in path :
C:\Program Files\Common Files\system\ado\msado.tlb
2. Import ADODB on your project
Imports ADODB.CursorOptionEnum
Imports ADODB.CursorLocationEnum
Imports ADODB.CommandTypeEnum
Imports ADODB.CursorTypeEnum
Imports ADODB.DataTypeEnum
Imports ADODB.RecordStatusEnum
Imports ADODB.RecordTypeEnum
Imports ADODB.LockTypeEnum
Imports ADODB.ObjectStateEnum
3. Here's the sample syntax :
Dim adaptor
Dim ds As DataSet = New DataSet()
Dim strsql As String
Dim RSSQLSERVER As ADODB.Recordset
ConnectSQLServer = New ADODB.Connection
ConnectSQLServer.Open("FILE NAME=" & My.Application.Info.DirectoryPath & "\SQLServer.udl")
' Create Object Recordset
RSSQLSERVER = New ADODB.Recordset
RSSQLSERVER.CursorLocation = adUseClient
strsql = "select * From mytable"
' Check Status Record set , already Open or Still Close
If RSSQLSERVER.State <> adStateClosed Then RSSQLSERVER.Close()
'FILL Recordset with data from Database
RSSQLSERVER.Open(strsql, ConnectSQLServer, adOpenKeyset, adLockOptimistic)
RSSQLSERVER.ActiveConnection = Nothing
ConnectSQLServer.Close()
'Check Record Count
If RSSQLSERVER.RecordCount > 0 Then
'if record found to do here
Else
'if no record to do here
End If
'this code below will populate the datagrid but will closed current active connection , so display the data first before using this code.
adaptor = New OleDbDataAdapter()
adaptor.Fill(ds, RSSQLSERVER, "mytable")
dataGridView1.Datasource = ds.Tables(0).DefaultView
works great ;)
Post a Comment
Harap gunakan bahasa yang baik dan sopan, terima kasih