VB.Net/Database ADO.net/Access Connection String — различия между версиями

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

Текущая версия на 12:44, 26 мая 2010

OleDbConnection Connection String for Access database

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Public Class MainClass
    Shared Sub Main()
      Dim OleDbConnection As System.Data.OleDb.OleDbConnection
      OleDbConnection = New System.Data.OleDb.OleDbConnection()
      OleDbConnection.ConnectionString = _
         "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";" & _
         "User ID=Admin;Data Source=Employee.mdb;Mode=Sha" & _
         "re Deny None;Extended Properties="""";" & _
         "Jet OLEDB:System database="""";Jet OLEDB:Regis" & _
         "try Path="""";Jet OLEDB:Database Password="""";" & _
         "Jet OLEDB:Engine Type=5;Jet OLEDB:Dat" & _
         "abase Locking Mode=1;Jet OLEDB:Global Partial " & _
         "Bulk Ops=2;Jet OLEDB:Global Bulk T" & _
         "ransactions=1;Jet OLEDB:New Database " & _
         "Password="""";Jet OLEDB:Create System Databas" & _
         "e=False;Jet OLEDB:Encrypt Database=False;" & _
         "Jet OLEDB:Don"t Copy Locale on Compact=" & _
         "False;Jet OLEDB:Compact Without Replica " & _
         "Repair=False;Jet OLEDB:SFP=False"

      Try
         OleDbConnection.Open()
         OleDbConnection.Close()
      Catch ex As Exception
         Console.WriteLine(ex.Message)
      End Try
    End Sub
    
End Class

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


Simple Connection String

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()
            "Fill the DataTable object
            objDataAdapter.Fill(objDataTable)
            "Load the list box on the form
            For Each objDataRow In objDataTable.Rows
                Console.WriteLine(objDataRow.Item("FirstName") & " " & _
                    objDataRow.Item("LastName"))
            Next
        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>