Imports System.Data.SqlClient
Public Class Form1 Inherits System.Windows.Forms.Form Dim myConnection As SqlConnection Dim myCommand As SqlCommand Dim dr As New SqlDataReader() 'declaring the objects Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs") 'establishing connection. you need to provide password for sql server Try myConnection.Open() 'opening the connection myCommand = New SqlCommand("Select * from discounts", myConnection) 'executing the command and assigning it to connection dr = myCommand.ExecuteReader() While dr.Read() 'reading from the datareader MessageBox.Show("discounttype" & dr(0).ToString()) MessageBox.Show("stor_id" & dr(1).ToString()) MessageBox.Show("lowqty" & dr(2).ToString()) MessageBox.Show("highqty" & dr(3).ToString()) MessageBox.Show("discount" & dr(4).ToString()) 'displaying the data from the table End While dr.Close() myConnection.Close() Catch e As Exception End Try End Sub End Class 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 http://www.worldbestlearningcenter.com/index_files/VB_NET_Databases_Access_ADO_Command_DataReader_Objects.htm |