1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| Imports System
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
' Set up connection string
Dim connString As String = _
"server = .\sqlexpress;" _
& "integrated security = true;" _
& "database = northwind"
' Set up query string
Dim sql As String = _
"select " _
& " * " _
& "from " _
& " employees"
' Declare connection and data reader variables
Dim conn As SqlConnection = Nothing
Dim rdr As SqlDataReader = Nothing
Try
' Open connection
conn = New SqlConnection(connString)
conn.Open()
' Execute query
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
rdr = cmd.ExecuteReader()
' Display output header
Console.WriteLine( _
"This program demonstrates the use of " _
& "the SQL Server Data Provider." _
& ControlChars.NewLine _
)
Console.WriteLine("First Name Last Name")
' Process result set
While rdr.Read
Console.WriteLine( _
"{0} | {1}" _
, rdr("firstname").ToString().PadLeft(10) _
, rdr(1).ToString().PadLeft(10) _
)
End While
Catch e As Exception
Console.WriteLine("Error: " & e.ToString)
Finally
' Close connection
rdr.Close()
conn.Close()
End Try
End Sub
End Module |
Partager