📄 concurrency-error.aspx
字号:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Catching Concurrency Errors When Updating the Source Data</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Catching Concurrency Errors When Updating the Source Data</span><hr />
<!--------------------------------------------------------------------------->
<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>
<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div>UPDATE command: <b><span id="outUpdate" runat="server"></span></b></div><p />
<b>Initial contents of the Books table:</b>
<asp:datagrid id="dgrResult1" runat="server" /><p />
<b><div id="outConcurrent" runat="server"></div></b><p />
<b>Contents of the Books table after editing:</b>
<asp:datagrid id="dgrResult2" runat="server" /><p />
<div id="outResult" runat="server"></div><p />
<div id="outError" runat="server"></div><p />
<script language="vb" runat="server">
Sub Page_Load()
'get connection string from ..\global\connect-strings.ascx user control
Dim strConnect = ctlConnectStrings.OLEDBConnectionString
outConnect.innerText = strConnect
'specify the SELECT statement to extract the data
Dim strSelect As String
strSelect = "SELECT * FROM BookList WHERE ISBN='1861001622'"
outSelect.innerText = strSelect 'and display it
'create a new DataSet object
Dim objDataSet As New DataSet()
'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(strConnect)
'create a new DataAdapter using the connection object and select statement
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect)
Try
'fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "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
'objDataSet.AcceptChanges()
'declare a variable to reference the Books table
Dim objTable As DataTable = objDataSet.Tables("Books")
'display the contents of the Books table before changing data
dgrResult1.DataSource = objTable.DefaultView
dgrResult1.DataBind() 'and bind (display) the data
'-------------------------------------------------------------------
'change a value in the original table while the DataSet is holding
'a disconnected copy of the data to force a concurrency error
Dim strUpdate As String
Dim datNow As DateTime = Now()
Dim strNow As String = datNow.ToString("dd-M-yy \a\t hh:mm:ss")
strUpdate = "UPDATE BookList SET Title = 'Book Written on " _
& strNow & "' WHERE ISBN = '1861001622'"
Dim intRowsAffected As Integer
Dim objNewConnect As New OleDbConnection(strConnect)
Dim objNewCommand As New OleDbCommand(strUpdate, objNewConnect)
Try
'open the connection to the database
objNewConnect.Open()
'execute the SQL statement and return the number of rows affected
intRowsAffected = objNewCommand.ExecuteNonQuery()
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
outConcurrent.InnerHtml = "Command object concurrently updated " _
& CStr(intRowsAffected) & " record(s)<br />" & strUpdate
'-------------------------------------------------------------------
'now change the record in the Books table
objTable.Rows(0)("Title") = "Amateur Theatricals for Windows 2000"
objTable.Rows(0)("PublicationDate") = Now()
'display the contents of the Books table after changing the data
dgrResult2.DataSource = objTable.DefaultView
dgrResult2.DataBind() 'and bind (display) the data
Try
'perform the update on the original data
'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 command for the DataAdapter
'(we don't need insert and delete commands in this example)
objDataAdapter.UpdateCommand = objCommandBuilder.GetUpdateCommand()
'*******************************************************************
'To suppress errors, uncomment next code line. It prevents exceptions
'being thrown, and allows the updates to continue on any other rows.
'The error details can then be obtained from the RowError property
'of the row that generated the error if required (see later).
'objDataAdapter.ContinueUpdateOnError = True
'*******************************************************************
'display the auto-generated UPDATE command statement
outUpdate.InnerText = objDataAdapter.UpdateCommand.CommandText
'now do the update
intRowsAffected = objDataAdapter.Update(objDataSet, "Books")
outResult.InnerHtml = "<b>* DataSet.Update</b> affected <b>" _
& CStr(intRowsAffected) & "</b> row(s).<p />" _
& "Value of <b>RowError</b> property for the modified row is:<br />" _
& "<b>" & objDataSet.Tables(0).Rows(0).RowError & "</b>."
Catch objError As Exception
'display error details
outError.innerHTML = "<b>* Error while updating original data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
End Try
End Sub
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -