VB.Net/Data Structure/Connection

Материал из VB Эксперт
Перейти к: навигация, поиск

Check the state of the connection and report appropriately

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Public Class MainClass
    Shared Sub Main()
        "Declare variables and objects
        Dim strConnectionString As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=Employee.mdb;"
        Dim objConnection As New OleDbConnection(strConnectionString)
        Dim strSQL As String = _
           "SELECT FirstName, LastName FROM Employee"
        Dim objCommand As New OleDbCommand(strSQL, objConnection)
        Dim objDataAdapter As New OleDbDataAdapter(objCommand)
        Dim objDataTable As New Data.DataTable("Employee")
        Dim objDataRow As DataRow
        Try
            "Open the database connection
            objConnection.Open()
            "Check the state of the connection and report appropriately
            If objConnection.State = ConnectionState.Open Then
                Console.WriteLine( "Database connection is open")
            Else
                Console.WriteLine("Database connection failed")
            End If
        Catch OleDbExceptionErr As OleDbException
            "Write the exception
            Console.WriteLine(OleDbExceptionErr.Message)
        Catch InvalidOperationExceptionErr As InvalidOperationException
            "Write the exception
            Console.WriteLine(InvalidOperationExceptionErr.Message)
        End Try
        "Close the database connection
        objConnection.Close()
        "Clean up
        objDataRow = Nothing
        objDataTable.Dispose()
        objDataTable = Nothing
        objDataAdapter.Dispose()
        objDataAdapter = Nothing
        objCommand.Dispose()
        objCommand = Nothing
        objConnection.Dispose()
        objConnection = Nothing
    End Sub
End Class

<A href="http://www.vbex.ru/Code/VBDownload/Employee.zip">Employee.zip( 7 k)</a>


Database Connection Properties: version, data source, WorkstationId, database and etc

Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
   Shared Sub Main()
      Dim connString As String = "server=(local)\SQLEXPRESS; " & _
         "integrated security=sspi;"
      Dim conn As New SqlConnection(connString)
      Try
         conn.Open()
         Console.WriteLine("Connection Opened")
         Console.WriteLine("Connection Properties")
         Console.WriteLine("- ConnectionString : {0}",conn.ConnectionString)
         Console.WriteLine("- Database : {0}",conn.Database)
         Console.WriteLine("- DataSource : {0}", conn.DataSource)
         Console.WriteLine("- ServerVersion : {0}", conn.ServerVersion)
         Console.WriteLine("- State : {0}", conn.State)
         Console.WriteLine("- WorkstationId : {0}", conn.WorkstationId)
      Catch ex As SqlException
         Console.WriteLine("Error: " & ex.ToString())
      Finally
         conn.Close()
         Console.WriteLine("Connection Closed")
      End Try
   End Sub
End Class