Imports System.Data.SqlClient
Public Class
Form1
Private
ConnString As String
Private Sub
Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'CnStr ="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program
Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf";Integrated
Security=True;User Instance=True"
End Sub
Public Sub
ReadMyData(ByVal myConnString As String)
Dim mySelectQuery As
String = "SELECT
ProductId, Name FROM Production.Product"
Dim myConnection As
New SqlConnection(myConnString)
Dim myCommand As
New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As
SqlDataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
Console.WriteLine((myReader.GetInt32(0) & ",
" & myReader.GetString(1)))
End While
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub 'ReadMyData
Private Sub
Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
Button1.Click
ConnString = "Data Source= .\SQLEXPRESS;Initial
Catalog = C:\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User
Instance=True;"
ReadMyData(ConnString)
End Sub
Private Sub
Button2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
Button2.Click
Me.Close()
End Sub
End Class