?? ??? ?? ??? ???? ????? ???? ?? ?? ????? ????? ??? ?? ???? Access ??????? ?? ??????? ???? ?? ??? ???? ?????
??? Jet ??????? ?? ????? ??? ??????? ?? ??? ?? ?? ????? ?? ?? SQL ?????, ??????? ??? Jet ??????? multi-statement ??? ???? ?? ?????? ???? ????? ?????? ??? OLE DB ??????? 4.0 ??????? ???????
??? @@ ??????????? ?? ???? ??????? ?? ????? ???? ??? ?? ?? ????:-?????? ????? ?? ??? ??????? ???? ?? ??? ???? ?????? ???? ??? ????? ?? ???
??? @@ ??????????? ?? ???, ?? ???????? ?? ?? ?? ???? ?????
OleDbCommand???????? ??? ?? ???? ????? ?? ?? ???? ?? ????? ?? ????? ???? ?? ???
OleDbCommand????? ????? ??? ?? ???? ???? ????
???::?? ?????? ???? Microsoft Jet OLEDB 4.0 ??????? ?? ??? ??? ???? ???
Microsoft Jet OLEDB ?? ????? ????????? ??? ?? ?????? ?? ?????? ???? ?????
Access ??????? ?? ?????? ????
Access ??????? ?? ?????? ?? ?? ????? ????? ?? ??? ?? ?????? ?????, ?? ????? ?? ???? ????:
- Microsoft Visual Studio .NET 2002 ?? ??????? ?????
- ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
- ?? ???????????????? ??????????? ????,????? ??? ?????????. ?? ?????????????????????, ????? ????????? ?????????. ???????? ??? ??, Module1.vb ?? ???? ???
- ????????? ?? ???MyJetApplication?? ????-????? ????, ?? ???? ???OK.
- ?????? ??? ?? ????? ??? ?? ???????????? ????:
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
- ???? ????? ?? ??? ???? Access ??????? ?? ????? ???? ?? ??? ??????? ???????? ??? ??????? ?????
????? ????? ??? trap
????? ?? ??? ?????? ?? ???
RowUpdatedDataAdapter ?????, ?? ?? ???? ??? trap ????? ????? ??? ?? ???? Access ??????? ??? ??? ?????? ?? ???? ????? ?? ??? ????? ??? ?????
RowUpdated????? ?? ???, ???? ?????
??? @@ ????????? ???? ?? ????? ?? ??????
????????????, ?? ???? ??? ?? ????? ????? ?? ??? ?????? ?????? ???? ??? ?? ?? ??? ????? ?????? ??? lastly,
AcceptChanges?? ??? ????
DataRowobject to accept the column value.
The trap the
identity column value, follow these steps:
- Add the following code before the?????method to create a secondOleDbCommandobject for theSELECT @@IDENTITYquery:
' Create OleDbCommand for SELECT @@IDENTITY statement
Private cmdGetIdentity As OleDbCommand
- ????? ??? ??????????method for creating a new instance ofOleDbCommand????:
' Create another command to get IDENTITY value.
cmdGetIdentity = New OleDbCommand()
cmdGetIdentity.CommandText = "SELECT @@IDENTITY"
cmdGetIdentity.Connection = cnJetDB
- ????? ??? ??????????method for handling theRowUpdated?????:
' Delegate for handling RowUpdated event.
AddHandler oleDa.RowUpdated, AddressOf HandleRowUpdated - ????? ??? ??????????method for updating the data. TheRowUpdatedevent is raised after calling the???????????? ???
' Update the Data
oleDa.Update(dtTest)
- ????? ??? ??????????method to drop theAutoIncrementTesttable and release the resources:
' 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 - ????? ??????RowUpdatedevent handler code to Module1:
' 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
- ????? ???????? ???????? ??,???????????????? ?????? Identity column values are
displayed in the Output window.
??? ??????? ?????
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 ???????? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
232144
(http://support.microsoft.com/kb/232144/EN-US/
)
?????: ??? OLE DB ??????? ??????? 4.0 ?? ?????? ???? SELECT @@ ?????
???? ??????? ?? ???, ?????????? MSDN ??? ???? ?? ????::
???? ID: 815629 - ????? ???????: 05 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
- Microsoft ADO.NET 1.1
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 Standard Edition
| kbjet kbprogramming kbtsql kbsystemdata kbhowtomaster kbmt KB815629 KbMthi |
???? ?????? ??????????????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:
815629
(http://support.microsoft.com/kb/815629/en-us/
)