代码如下:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim connect_string As New dbaction()
gstrConnect = connect_string.ole_string 'specify the SELECT statement to extract the data
Dim strSelect As String
strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610022%'" 'create a new DataSet object
gobjDataSet = New DataSet() 'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(gstrConnect) 'create a new DataAdapter using the connection object and select statement
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect) Try 'fill the dataset with data using the DataAdapter object
objDataAdapter.Fill(gobjDataSet, "Books") Catch objError As Exception 'display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution End Try 'accept the changes to "fix" the current state of the DataSet contents
gobjDataSet.AcceptChanges() 'declare a variable to reference the Books table
Dim objTable As DataTable = gobjDataSet.Tables("Books") 'display the contents of the Books table before changing data
dgrResult1.DataSource = objTable.DefaultView
dgrResult1.DataBind() 'and bind (display) the data 'now change some records in the Books table
objTable.Rows(0).Delete()
objTable.Rows(1)("Title") = "Amateur Theatricals for Windows 2000"
objTable.Rows(2).Delete()
objTable.Rows(3).Delete()
objTable.Rows(4)("PublicationDate") = "01-01-2002"
objTable.Rows.RemoveAt(5)
'notice that using the Remove method on row 5 (rather than ing
'it as deleted) means that the next row then becomes row 5
objTable.Rows(5)("ISBN") = "200000000" 'add a new row using an array of values
Dim objValsArray(2) As Object
objValsArray(0) = "200000001"
objValsArray(1) = "Impressionist Guide to Painting Computers"
objValsArray(2) = "05-02-2002"
objTable.Rows.Add(objValsArray) 'display the contents of the Books table after changing the data
dgrResult2.DataSource = objTable.DefaultView
dgrResult2.DataBind() 'and bind (display) the data 'now set up event handler to react to RowUpdated event
AddHandler objDataAdapter.RowUpdated, _
New OleDbRowUpdatedEventHandler(AddressOf OnRowUpdated)
Dim objTransaction As OleDbTransaction Try 'create an auto-generated command builder to create the commands
'to update, insert and delete the data
Dim objCommandBuilder As New OleDbCommandBuilder(objDataAdapter) 'set the update, insert and delete commands for the DataAdapter
objDataAdapter.DeleteCommand = objCommandBuilder.GetDeleteCommand()
objDataAdapter.InsertCommand = objCommandBuilder.GetInsertCommand()
objDataAdapter.UpdateCommand = objCommandBuilder.GetUpdateCommand() 'start a transaction so that we can roll back the changes
'must do this on an open Connection object
objConnect.Open()
objTransaction = objConnect.BeginTransaction() 'attach the current transaction to all the Command objects
'must be done after setting Connection property
objDataAdapter.DeleteCommand.Transaction = objTransaction
objDataAdapter.InsertCommand.Transaction = objTransaction
objDataAdapter.UpdateCommand.Transaction = objTransaction 'perform the update on the original data
objDataAdapter.Update(gobjDataSet, "Books") Catch objError As Exception 'rollback the transaction undoing any updates
objTransaction.Rollback() 'display error details
outError.InnerHtml = "<b>* Error while updating original data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution End Try
objTransaction.commit() Sub OnRowUpdated(ByVal objSender As Object, ByVal objArgs As OleDbRowUpdatedEventArgs)
在此根据objArgs.RecordsAffected来判断update是否有用,及是否并发!
end sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim connect_string As New dbaction()
gstrConnect = connect_string.ole_string 'specify the SELECT statement to extract the data
Dim strSelect As String
strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610022%'" 'create a new DataSet object
gobjDataSet = New DataSet() 'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(gstrConnect) 'create a new DataAdapter using the connection object and select statement
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect) Try 'fill the dataset with data using the DataAdapter object
objDataAdapter.Fill(gobjDataSet, "Books") Catch objError As Exception 'display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution End Try 'accept the changes to "fix" the current state of the DataSet contents
gobjDataSet.AcceptChanges() 'declare a variable to reference the Books table
Dim objTable As DataTable = gobjDataSet.Tables("Books") 'display the contents of the Books table before changing data
dgrResult1.DataSource = objTable.DefaultView
dgrResult1.DataBind() 'and bind (display) the data 'now change some records in the Books table
objTable.Rows(0).Delete()
objTable.Rows(1)("Title") = "Amateur Theatricals for Windows 2000"
objTable.Rows(2).Delete()
objTable.Rows(3).Delete()
objTable.Rows(4)("PublicationDate") = "01-01-2002"
objTable.Rows.RemoveAt(5)
'notice that using the Remove method on row 5 (rather than ing
'it as deleted) means that the next row then becomes row 5
objTable.Rows(5)("ISBN") = "200000000" 'add a new row using an array of values
Dim objValsArray(2) As Object
objValsArray(0) = "200000001"
objValsArray(1) = "Impressionist Guide to Painting Computers"
objValsArray(2) = "05-02-2002"
objTable.Rows.Add(objValsArray) 'display the contents of the Books table after changing the data
dgrResult2.DataSource = objTable.DefaultView
dgrResult2.DataBind() 'and bind (display) the data 'now set up event handler to react to RowUpdated event
AddHandler objDataAdapter.RowUpdated, _
New OleDbRowUpdatedEventHandler(AddressOf OnRowUpdated)
Dim objTransaction As OleDbTransaction Try 'create an auto-generated command builder to create the commands
'to update, insert and delete the data
Dim objCommandBuilder As New OleDbCommandBuilder(objDataAdapter) 'set the update, insert and delete commands for the DataAdapter
objDataAdapter.DeleteCommand = objCommandBuilder.GetDeleteCommand()
objDataAdapter.InsertCommand = objCommandBuilder.GetInsertCommand()
objDataAdapter.UpdateCommand = objCommandBuilder.GetUpdateCommand() 'start a transaction so that we can roll back the changes
'must do this on an open Connection object
objConnect.Open()
objTransaction = objConnect.BeginTransaction() 'attach the current transaction to all the Command objects
'must be done after setting Connection property
objDataAdapter.DeleteCommand.Transaction = objTransaction
objDataAdapter.InsertCommand.Transaction = objTransaction
objDataAdapter.UpdateCommand.Transaction = objTransaction 'perform the update on the original data
objDataAdapter.Update(gobjDataSet, "Books") Catch objError As Exception 'rollback the transaction undoing any updates
objTransaction.Rollback() 'display error details
outError.InnerHtml = "<b>* Error while updating original data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution End Try
objTransaction.commit() Sub OnRowUpdated(ByVal objSender As Object, ByVal objArgs As OleDbRowUpdatedEventArgs)
在此根据objArgs.RecordsAffected来判断update是否有用,及是否并发!
end sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'need a new (separate) Connection and Command object
Dim connect_string As New dbaction()
Dim gstrConnect As String = connect_string.ole_string
Dim objNewConnect As New OleDbConnection(gstrConnect)
Dim objNewCommand As New OleDbCommand()
objNewCommand.Connection = objNewConnect
Dim strConcurrent, strUpdate As String
Dim intRowsAffected As Integer
Try objNewConnect.Open() 'modify two of the book titles to force concurrency errors
'strConcurrent += "<b>Concurrently executed</b>:<br />"
Dim datNow As DateTime = Now()
Dim strNow As String = datNow.ToString("dd-M-yy \a\t hh:mm:ss")
strUpdate = "UPDATE BookList SET Title = 'New Book Written on " _
& strNow & "' WHERE ISBN = '186100222X'"
objNewCommand.CommandText = strUpdate
intRowsAffected = objNewCommand.ExecuteNonQuery()
'strConcurrent += strUpdate & "<br /> ... <b>" _
'& CStr(intRowsAffected) & "</b> row(s) affected<br />" 'edit the next line to force a concurrency error if none occurs
strUpdate = "UPDATE BookList SET Title = 'Another Book Written on " _
& strNow & "' WHERE ISBN = '1861002262'"
objNewCommand.CommandText = strUpdate
intRowsAffected = objNewCommand.ExecuteNonQuery()
strConcurrent += strUpdate & "<br /> ... <b>" _
& CStr(intRowsAffected) & "</b> row(s) affected<br />" objNewConnect.Close() Catch objError As Exception 'display error details
'outError.InnerHtml = "<b>* Error while updating original data</b>.<br />" _
' & objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution End Try End Sub