Dim conn As New SqlConnection() Dim comm As New SqlCommand() Dim dr As SqlDataReader Dim connString As String Dim customerName As String Try connString = "Initial Catalog=Northwind;" connString += "Data Source=Office1\Test;" connString += "uid=test;pwd=password;" conn = New SqlConnection(connString)
With comm .Connection = conn .CommandText = "SELECT * FROM Northwind.dbo.Customers" .CommandType = CommandType.Text conn.Open() dr = .ExecuteReader() End With
While (dr.Read()) If Not (dr.Item(2) Is System.DBNull.Value) Then customerName = CStr(dr.Item(2)) End If End While
Catch ex As SqlException System.Console.WriteLine("SQL Server Error: " + ex.Message + ex.StackTrace)
Catch ex As Exception System.Console.WriteLine("Exception: " + ex.Message + ex.StackTrace)
Finally If Not (conn Is Nothing) Then If (conn.State = ConnectionState.Open) Then conn.Close() End If conn.Dispose() End If
If Not (dr Is Nothing) Then If Not (dr.IsClosed) Then dr.Close() End If End If If Not (comm Is Nothing) Then comm.Dispose() End If End Try |