0% found this document useful (0 votes)
39 views3 pages

Form1 Object Eventargs: Imports Imports Public Class Private Sub Byval As Byval As Handles Mybase Dim As String

This code connects to an MS Access database, executes a SQL query to select data where the address is equal to "Ktm", reads the results into a data reader, and displays the first name field of each returned row in a text box. It catches any exceptions and closes the connection.

Uploaded by

Uttam Shrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Form1 Object Eventargs: Imports Imports Public Class Private Sub Byval As Byval As Handles Mybase Dim As String

This code connects to an MS Access database, executes a SQL query to select data where the address is equal to "Ktm", reads the results into a data reader, and displays the first name field of each returned row in a text box. It catches any exceptions and closes the connection.

Uploaded by

Uttam Shrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports System.Data.

OleDb
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cnString As String
txtStatus.Text = "connecting Ms Access" & vbNewLine
cnString = "Provider=Microsoft.Jet.OleDB.4.0;Persist Security
Info=False;Data Source=" &
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) &
"\nha\data.mdb"
'create command
Dim sqlQRY As String = "Select * from USERINFO where Address='Ktm' "
'create connection
Dim conn As OleDbConnection = New OleDbConnection(cnString)
Try
'create connection
conn.Open()
'oledbcommand
Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
'create data reader
Dim rdr As OleDbDataReader = cmd.ExecuteReader
While (rdr.Read)
txtStatus.Text = txtStatus.Text & rdr("Fname").ToString() &
vbNewLine
End While

Catch ex As Exception
txtStatus.Text = "Error:" & ex.ToString & vbNewLine
Finally
conn.Close()
txtStatus.Text = txtStatus.Text & "Connection closed"
End Try
End Sub
End Class

Imports System.Data.OleDb

Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cnString As String
txtStatus.Text = "connecting Ms Access" & vbNewLine
cnString = "Provider=Microsoft.Jet.OleDB.4.0;Persist Security
Info=False;Data Source=" &
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) &
"\nha\data.mdb"
'create command
Dim sqlQRY As String = "Select * from USERINFO where Address='Ktm' "
'create connection
Dim conn As OleDbConnection = New OleDbConnection(cnString)
Try
'create connection
conn.Open()
'oledbcommand
Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
'create data reader
Dim rdr As OleDbDataReader = cmd.ExecuteReader
While (rdr.Read)
txtStatus.Text = txtStatus.Text & rdr("Fname").ToString() &
vbNewLine
End While

Catch ex As Exception
txtStatus.Text = "Error:" & ex.ToString & vbNewLine
Finally
conn.Close()
txtStatus.Text = txtStatus.Text & "Connection closed"
End Try
End Sub
End Class

You might also like