Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace SimpleSql1
Class [Select]
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
<STAThread()> _Overloads Shared Sub Main(ByVal args() As String)
' we will first do the same thing
'* connecting to the database as we did before'
Dim conn As New SqlConnection("Data Source=computer_name;" + "Initial Catalog=simplesql;" + "User ID=sa;" + "Password=pass;")
Dim dr As SqlDataReader
Try
Dim cmd As New SqlCommand
cmd.CommandTimeout = 60
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT Count(*) FROM simplesql"
conn.Open()
If conn.State = ConnectionState.Open Then
Dim objCount As Object = cmd.ExecuteScalar()
Dim iCount As Integer = CInt(objCount)
Console.WriteLine("Count was succesfull " + ControlChars.Lf)
cmd.CommandText = "SELECT simple_id, simple_text FROM simplesql ORDER BY simple_id"
dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
Console.WriteLine("Succesfully Selected " + ControlChars.Lf)
' For loop to read everything from the table
Dim i As Integer
For i = 0 To iCount - 1
dr.Read() ' Read one row from the table
Console.WriteLine("ID: {0} " + ControlChars.Tab + " Text: {1}", dr(0), dr(1))
Next i
End If
Catch exp As Exception
Console.Write(exp.Message)
Finally
conn.Close()
End Try
Console.WriteLine(ControlChars.Lf + ControlChars.Lf + " Press any key to quite")
Console.Read()
End Sub 'Main
End Class '[Select]
End Namespace 'SimpleSql1