ADO.NET

Command and DataReader Objects

You can use the Command object to retrieve data from a database after you connected to it by using the Connection object. The OleDbCommand class is used to construct the Command object. You will supply the SQL selection statement and the Connecton object. The The data will be stored in the DataReader object by executing the ExcecuteReader method of the OleDbCommand class. Here is an example code that uses OleDbCommand and OleDbDataReader to read an Ms. Access database called databasedic.accdb.

If you will get the data from SQLServer by using the Command and DataReader, you simply need to change from OleDbCommand and OleDbDataReader to SqlCommand and SqlDataReader. You also need to change the connection string to connect to the SQLServer. You might want to read this page to learn to connect SQLServer database.

Imports System.Data.OleDb
Module Module1

Sub Main()
Dim con As OleDbConnection = Nothing
Dim cmd As OleDbCommand = Nothing
Dim reader As OleDbDataReader = Nothing

Try

con = New OleDbConnection()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Environment.CurrentDirectory & "\\" & "databasedic.accdb"
con.Open()
cmd = New OleDbCommand("Select * From Dictbl", con)
reader = cmd.ExecuteReader()

While reader.Read()

Console.WriteLine(reader(0).ToString())
End While

Catch e As Exception
Console.WriteLine("Error")
Finally

con.Close()
End Try
Console.Read()
End Sub

End Module

 

VB.NET command and data reader retrieving data from database access




HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.