' File: http://www.garlen.net/SampleCode/Console_SQL_DataReader_Via_Command.vb.txt ' (re)written By: Dan Garlen 3/21/04 imports System imports System.data imports sql = System.Data.SqlClient public module MyModule sub Main Console_SQL_DataReader_Via_Command AnyKey end sub Private Sub Console_SQL_DataReader_Via_Command Dim strSQL as String = "user id=sa;integrated security=SSPI;data source='(local)';initial catalog=Northwind" Console.WriteLine("Connect String: ", strSQL) Dim cn AS new sql.SqlConnection cn.ConnectionString = strSQL Try ' Setup Command Dim cmd as new sql.SqlCommand cmd.CommandText = "SELECT [CompanyName] FROM [Customers]" cmd.CommandType = CommandType.Text cmd.Connection = cn Dim dr as sql.sqlDataReader cn.Open dr = cmd.ExecuteReader Do While dr.Read = True 'The following will all return the same results Console.WriteLine("Company Name: [" + dr.Item(0).ToString + "]") 'Console.WriteLine("Company Name: [" + dr.Item("CompanyName").ToString + "]") 'Console.WriteLine("Company Name: [" + dr.GetString(0) + "]") Loop dr.Close() cn.Close() Catch ex As Exception Console.WriteLine("Error", ex.Message.tostring) End Try End Sub sub AnyKey Console.WriteLine("") Console.Write("Press enter key to continue... ") Console.ReadLine() end Sub end module