concurrency-error.aspx

来自「Professional ASP.NET source code」· ASPX 代码 · 共 166 行

ASPX
166
字号
<%@Page Language="C#"%>

<%@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="C#" runat="server">

	void Page_Load(Object sender, EventArgs e)
	{

		// get connection string from ..\global\connect-strings.ascx user control
		string strConnect = ctlConnectStrings.OLEDBConnectionString;
		outConnect.InnerText = strConnect;

		// specify the SELECT statement to extract the data
		string strSelect = "SELECT * FROM BookList WHERE ISBN='1861001622'";
		outSelect.InnerText = strSelect;   // and display it

		// create a new DataSet object
		DataSet objDataSet = new DataSet();

		// create a new Connection object using the connection string
		OleDbConnection objConnect = new OleDbConnection(strConnect);

		// create a new DataAdapter using the connection object and select statement
		OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);

		try
		{
			// fill the dataset with data from the DataAdapter object
			objDataAdapter.Fill(objDataSet, "Books");
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
						+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

		// accept the changes to "fix" the current state of the DataSet contents
		// objDataSet.AcceptChanges();

		// declare a variable to reference the Books table
		DataTable objTable = 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
		string strUpdate;
		DateTime datNow = DateTime.Now;
		string strNow = datNow.ToString("dd-M-yy \\a\\t hh:mm:ss");
		strUpdate = "UPDATE BookList SET Title = 'Book Written on "
							+ strNow + "' WHERE ISBN = '1861001622'";

		int intRowsAffected;
		OleDbConnection objnewConnect = new OleDbConnection(strConnect);
		OleDbCommand objnewCommand = 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 (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while updating original data</b>.<br />"
							+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

		outConcurrent.InnerHtml = "Command object concurrently updated "
					+ intRowsAffected.ToString() + " 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"] = DateTime.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
			OleDbCommandBuilder objCommandBuilder = 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>"
								+ intRowsAffected.ToString() + "</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 (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while updating original data</b>.<br />"
							+ objError.Message + "<br />" + objError.Source;
		}

	}
	
</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?