HOW TO: ??????? ???? ???? ????? ??????? ??????? ?? ????? ?????? Access ???????? Visual Basic .NET

?????? ????????? ?????? ?????????
???? ???????: 815629 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

?????? ??? ??????? ???? ????? ??? ????? ??????? ???? ???? ?????? ?? ????? ?????? Access.

??????? ???? ?????? ?? ????? ?????? Jet ????? ?? SQL Server ??? ????? ?????? Jet ?? ????? ??????? ??????? multi-statement. ????? Jet OLE DB ?????? 4.0 ???? ????????? SELECT @@ ?????? ???? ???? ?? ??????? ???? ????? ????? ???????? ???? ?? ?????? ?? ???????. ?????? ????????? SELECT @@ ?????? ?? ???????? ??????? ???? OleDbCommand ???. ???? ??? ??????? ????? ??????? ???? OleDbCommand ??????? ???? ???? ??????.

??????:???? ??? ?????? ??? ?? ????? ?????? Microsoft Jet 4.0 OLEDB. ?? ????? ????????? ??????? ?? Microsoft Jet OLEDB ??? ??????.

??????? ????

??????? ?????? ?????? Access

??????? ?????? ?????? Access ?????? ???? ?? ???? ???? ???? ??????? ???????:
  1. ??? ????? Microsoft Visual Studio .NET 2002.
  2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
  3. ??? ????? Project ? ???? ??? ?????? Visual Basic. ??? ?????? ????? ???? ??? ????? ???? ??????. ?????????? ??? ????? Module1.vb.
  4. ??? ??????? MyJetApplication ?? ???? ??? ?????.
  5. ??????? ????????? ???????? ???????? ?? ????????? ???????? ???????:
    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    
    Module Module1
    
       Sub Main()
    
          ' Open Connection 
          Dim cnJetDB As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourAccessDatabase")
          cnJetDB.Open()
    
          ' If the test table does not exist, create the Table.
          Dim strSQL As String
          strSQL = "CREATE TABLE AutoIncrementTest " & _
                   "(ID int identity, Description varchar(40), " & _
                   "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"
    
          ' Command for creating Table.
          Dim cmdJetDB As New OleDbCommand(strSQL, cnJetDB)
          cmdJetDB.ExecuteNonQuery()
    
          ' Create a DataAdaptor With Insert Command For inserting records
          Dim oleDa As New OleDbDataAdapter("Select * from AutoIncrementTest", cnJetDB)
    
    
          ' Command to Insert Records.
          Dim cmdInsert As New OleDbCommand()
          cmdInsert.CommandText = "INSERT INTO AutoIncrementTest (Description) VALUES (?)"
          cmdInsert.Connection = cnJetDB
          cmdInsert.Parameters.Add(New OleDbParameter("Description", OleDbType.VarChar, 40, "Description"))
          oleDa.InsertCommand = cmdInsert
    
          ' Create a DataTable
          Dim dtTest As New DataTable()
          oleDa.Fill(dtTest)
    
          Dim drTest As DataRow
    
          ' Add Rows to the Table
          drTest = dtTest.NewRow
          drTest("Description") = "This is a Test Row 1"
          dtTest.Rows.Add(drTest)
    
          drTest = dtTest.NewRow
          drTest("Description") = "This is a Test Row 2"
          dtTest.Rows.Add(drTest)
          
       End Sub
    
    End Module
    
  6. ????? ??? "???? ????????" ?? ????? ??????? ??? ??? ????? ?????? Access ????? ??.




????? ???? ???? ??????

?????? ??????? ???????? ?? ????? RowUpdated DataAdapter, ????? ????? ???? ???? ?????? ???? ??? ??????? ?????? ?? ???? ?? ????? ?????? Access. ?? ????? RowUpdated ???? ????? ????????? SELECT @@ IDENTITY ???????? ???? ??? ??? ??? ?? ????? ?????? ?????? ???? ?? ??????? ?????? ????????? ??? ???? ????. ??????? ? ??????? ??????? AcceptChanges ?????? DataRow ????? ???? ??????.

?????? ???? ???? ?????? ???? ??????? ???????:
  1. ?? ?????? ????????? ???????? ??????? ??? ??????? ??????? ?????? ???? OleDbCommand ???? ????????? SELECT @@ IDENTITY:
       ' Create OleDbCommand for SELECT @@IDENTITY statement
       Private cmdGetIdentity As OleDbCommand
  2. ????? ???????? ???????? ??????? ??? ??????? ??????? ?????? ???? ???? ?? ??? OleDbCommand:
          ' Create another command to get IDENTITY value.
          cmdGetIdentity = New OleDbCommand()
          cmdGetIdentity.CommandText = "SELECT @@IDENTITY"
          cmdGetIdentity.Connection = cnJetDB
    
    
  3. ????? ???????? ???????? ??????? ??? ??????? ??????? ??????? ??????? RowUpdated:
          ' Delegate for handling RowUpdated event.
          AddHandler oleDa.RowUpdated, AddressOf HandleRowUpdated
  4. ????? ???????? ???????? ??????? ??? ??????? ??????? ?????? ????????. ????? ????? RowUpdated ??? ??????? ????? ???????.
          ' Update the Data
          oleDa.Update(dtTest)
    
  5. ????? ???????? ???????? ??????? ??? ??????? ??????? ????? ?????? AutoIncrementTest ????? ???????:
          ' Drop the table
          cmdJetDB.CommandText = "DROP TABLE AutoIncrementTest"
          cmdJetDB.ExecuteNonQuery()
    
          ' Release the resources.
          cmdGetIdentity.Dispose()
          cmdGetIdentity = Nothing
          cmdInsert.Dispose()
          cmdInsert = Nothing
          cmdJetDB.Dispose()
          cmdJetDB = Nothing
          cnJetDB.Close()
          cnJetDB.Dispose()
          cnJetDB = Nothing
  6. ????? ??? ????? ????? RowUpdated ??????? ??? ?????? ??????? 1:
       ' Event handler for RowUpdated event.
       Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As OleDbRowUpdatedEventArgs)
          If e.Status = UpdateStatus.Continue AndAlso e.StatementType = StatementType.Insert Then
             ' Get the Identity column value
             e.Row("ID") = Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString())
             Debug.WriteLine(e.Row("ID"))
             e.Row.AcceptChanges()
          End If
       End Sub
    
  7. ?? ??????? ????? ? ???? ??? "????" ?????? ???????. ??? ??? ??? ?????? ???? ?? ?????? ?????.


????? ??? ????????? ????????

Imports System
Imports System.Data
Imports System.Data.OleDb

Module Module1

   ' Create OleDbCommand for SELECT @@IDENTITY statement
   Private cmdGetIdentity As OleDbCommand

   Sub Main()

      ' Open Connection 
      Dim cnJetDB As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourAccessDatabase")
      cnJetDB.Open()

      ' If the test table does not exist then create the Table
      Dim strSQL As String
      strSQL = "CREATE TABLE AutoIncrementTest " & _
               "(ID int identity, Description varchar(40), " & _
               "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"

      ' Command for Creating Table
      Dim cmdJetDB As New OleDbCommand(strSQL, cnJetDB)
      cmdJetDB.ExecuteNonQuery()

      ' Create a DataAdaptor With Insert Command For inserting records
      Dim oleDa As New OleDbDataAdapter("Select * from AutoIncrementTest", cnJetDB)


      ' Command to Insert Records
      Dim cmdInsert As New OleDbCommand()
      cmdInsert.CommandText = "INSERT INTO AutoIncrementTest (Description) VALUES (?)"
      cmdInsert.Connection = cnJetDB
      cmdInsert.Parameters.Add(New OleDbParameter("Description", OleDbType.VarChar, 40, "Description"))
      oleDa.InsertCommand = cmdInsert

      ' Create a DataTable
      Dim dtTest As New DataTable()
      oleDa.Fill(dtTest)

      Dim drTest As DataRow

      ' Add Rows to the Table
      drTest = dtTest.NewRow
      drTest("Description") = "This is a Test Row 1"
      dtTest.Rows.Add(drTest)

      drTest = dtTest.NewRow
      drTest("Description") = "This is a Test Row 2"
      dtTest.Rows.Add(drTest)

      ' Create another Command to get IDENTITY Value
      cmdGetIdentity = New OleDbCommand()
      cmdGetIdentity.CommandText = "SELECT @@IDENTITY"
      cmdGetIdentity.Connection = cnJetDB

      ' Delegate for Handling RowUpdated event
      AddHandler oleDa.RowUpdated, AddressOf HandleRowUpdated

      ' Update the Data
      oleDa.Update(dtTest)

      ' Drop the table
      cmdJetDB.CommandText = "DROP TABLE AutoIncrementTest"
      cmdJetDB.ExecuteNonQuery()

      ' Release the Resources
      cmdGetIdentity.Dispose()
      cmdGetIdentity = Nothing
      cmdInsert.Dispose()
      cmdInsert = Nothing
      cmdJetDB.Dispose()
      cmdJetDB = Nothing
      cnJetDB.Close()
      cnJetDB.Dispose()
      cnJetDB = Nothing
   End Sub

   ' Event Handler for RowUpdated Event
   Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As OleDbRowUpdatedEventArgs)
      If e.Status = UpdateStatus.Continue AndAlso e.StatementType = StatementType.Insert Then
         ' Get the Identity column value
         e.Row("ID") = Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString())
         Debug.WriteLine(e.Row("ID"))
         e.Row.AcceptChanges()
      End If
   End Sub
End Module

?????

?????? ??? ??????? ?????? ??? ??? ????????? ???????? ?? Visual Basic 6.0 ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
232144INFO: Jet OLE DB ?????? ??????? 4.0 ???? SELECT @@ ??????
????? ?? ?????????? ?? ?????? ???? MSDN ?????? ??? ?????:
??? OleDbDataAdapter.RowUpdated
http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.rowupdated(vs.71).aspx

???????

???? ???????: 815629 - ????? ??? ??????: 25/???? ??????/1428 - ??????: 3.2
????? ???
  • Microsoft ADO.NET 1.0
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
????? ??????: 
kbmt kbjet kbprogramming kbtsql kbsystemdata kbhowtomaster KB815629 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????815629

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com