complex-dataset-update.aspx

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

ASPX
246
字号
<%@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>Updating Complex Data with a DataSet and Stored Procedures</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Updating Complex Data with a DataSet and Stored Procedures</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>
<div>INSERT command: <b><span id="outInsert" runat="server"></span></b></div>
<div>DELETE command: <b><span id="outDelete" runat="server"></span></b></div>

<div id="outError" runat="server">&nbsp;</div>

<b>Initial contents of the Books table:</b>
<asp:datagrid id="dgrResult1" runat="server" /><p />
<b>Contents of the Books table after editing:</b>
<asp:datagrid id="dgrResult2" runat="server" /><p />

<b>Note:</b> while this page does execute the Update method of the DataSet, it does not<br />
actually change the original data. It uses a transaction that is rolled back afterwards.</br />
Otherwise, the update would prevent the code from running the next time.

<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; // and display it

		// specify the SELECT statement to extract the data
		string strSelect = "SELECT BookList.*, BookAuthors.FirstName, BookAuthors.LastName "
					+ "FROM BookList INNER JOIN BookAuthors ON BookList.ISBN = BookAuthors.ISBN "
					+ "WHERE BookList.ISBN LIKE '18610033%'";
		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

		// now change some records in the Books table
		objTable.Rows[0].Delete();
		objTable.Rows[1]["Title"] = "Amateur Theatricals for Windows 2000";
		objTable.Rows[1]["PublicationDate"] = "01-01-2003";
		objTable.Rows[1]["FirstName"] = "Priscilla";
		objTable.Rows[1]["LastName"] = "Prince";
		objTable.Rows[2].Delete();

		// add a new row using an array of values
		Object[] objValsArray = new Object[5];
		objValsArray[0] = "200000001";
		objValsArray[1] = "Impressionist Guide to Painting Computers";
		objValsArray[2] = "05-02-2002";
		objValsArray[3] = "Mark";
		objValsArray[4] = "Lard";
		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 have to create Command objects for the DataSetCommand to
		// use to update the original data. First the Update command
		OleDbCommand objUpdateCommand = new OleDbCommand("BookAuthorUpdate", objConnect);
		objUpdateCommand.CommandType = CommandType.StoredProcedure;

		// now create the Parameter objects and add to the Command object
		OleDbParameter objParam;
		objParam = objUpdateCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "ISBN";
		objParam.SourceVersion = DataRowVersion.Original;   // used in SQL WHERE clause
		objParam = objUpdateCommand.Parameters.Add("Title", OleDbType.VarChar, 100);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "Title";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objUpdateCommand.Parameters.Add("PublicationDate", OleDbType.DBDate);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "PublicationDate";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objUpdateCommand.Parameters.Add("FirstName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "FirstName";
		objParam.SourceVersion = DataRowVersion.Current ;  // used in SQL SET clause
		objParam = objUpdateCommand.Parameters.Add("LastName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "LastName";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause

		// now specify this Command object as the UpdateCommand
		objDataAdapter.UpdateCommand = objUpdateCommand;

		// next, the InsertCommand
		OleDbCommand objInsertCommand = new OleDbCommand("BookAuthorInsert", objConnect);
		objInsertCommand.CommandType = CommandType.StoredProcedure;
		objParam = objInsertCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "ISBN";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objInsertCommand.Parameters.Add("Title", OleDbType.VarChar, 100);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "Title";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objInsertCommand.Parameters.Add("PublicationDate", OleDbType.DBDate);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "PublicationDate";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objInsertCommand.Parameters.Add("FirstName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "FirstName";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objParam = objInsertCommand.Parameters.Add("LastName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "LastName";
		objParam.SourceVersion = DataRowVersion.Current;   // used in SQL SET clause
		objDataAdapter.InsertCommand = objInsertCommand;

		// next, the DeleteCommand
		OleDbCommand objDeleteCommand = new OleDbCommand("BookAuthorDelete", objConnect);
		objDeleteCommand.CommandType = CommandType.StoredProcedure;
		objParam = objDeleteCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "ISBN";
		objParam.SourceVersion = DataRowVersion.Original;   // used in SQL WHERE clause
		objParam = objDeleteCommand.Parameters.Add("FirstName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "FirstName";
		objParam.SourceVersion = DataRowVersion.Original;   // used in SQL WHERE clause
		objParam = objDeleteCommand.Parameters.Add("LastName", OleDbType.VarChar, 50);
		objParam.Direction = ParameterDirection.Input;
		objParam.SourceColumn = "LastName";
		objParam.SourceVersion = DataRowVersion.Original;   // used in SQL WHERE clause
		objDataAdapter.DeleteCommand = objDeleteCommand;

		// display the SQL statements that we created for the DataSet to use
		string strSQL;  // create a new string to store vales

		// get stored proc name and source column names for each parameter
		strSQL = objDataAdapter.UpdateCommand.CommandText;
		foreach (OleDbParameter objP in objDataAdapter.UpdateCommand.Parameters)
			strSQL += " @" + objP.SourceColumn + ",";

		strSQL = strSQL.Substring(1, strSQL.Length - 1);   // remove trailing comma
		outUpdate.InnerText = strSQL;   // and display it

		// repeat the process for the Insert command
		strSQL = objDataAdapter.InsertCommand.CommandText;
		foreach (OleDbParameter objP in objDataAdapter.InsertCommand.Parameters)
			strSQL += " @" + objP.SourceColumn + ",";

		strSQL = strSQL.Substring(1, strSQL.Length - 1);   // remove trailing comma
		outInsert.InnerText = strSQL;

		// repeat the process for the Delete command
		strSQL = objDataAdapter.DeleteCommand.CommandText;
		foreach (OleDbParameter objP in objDataAdapter.DeleteCommand.Parameters)
			strSQL += " @" + objP.SourceColumn + ",";

		strSQL = strSQL.Substring(1, strSQL.Length - 1);   // remove trailing comma
		outDelete.InnerText = strSQL;

		// declare a variable to hold a Transaction object
		OleDbTransaction objTransaction = null;

		try
		{

			// now we're finally ready to do the update itself
			// start a transaction so that we can roll back the changes
			// must do this on an open Connection object
			objConnect.Open();
			objTransaction = objConnect.BeginTransaction();

			// perform the update on the original data
			objInsertCommand.Transaction = objTransaction;
			objUpdateCommand.Transaction = objTransaction;
			objDeleteCommand.Transaction = objTransaction;
			objDataAdapter.Update(objDataSet, "Books");

			// to actually update the source tables just change the next line
			// to CommitTransaction() or remove the transaction altogether.
			// note that you will then have to change the SELECT statement and
			// code to be able to run the example again, or rebuild the
			// original data from the supplied SQL scripts
			objTransaction.Rollback();
		}
		catch (Exception objError)
		{
			objTransaction.Rollback();
			// display error details
			outError.InnerHtml = "<b>* Error while updating original data source</b>.<br />"
							+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

	}
</script>

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

⌨️ 快捷键说明

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