update-with-command.aspx

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

ASPX
122
字号
<%@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 Data with a Command Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Updating Data with a Command Object</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>SQL statement: <b><span id="outSQL" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResult" runat="server"></div>

<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 SQL statement to update the data
		DateTime datNow = DateTime.Now;
		string strNow = datNow.ToString("dd-M-yy \a\t hh:mm:ss");
		string strSQL = "UPDATE BookList SET Title = 'new Book Written on "
						+ strNow + "' WHERE ISBN='1861003005'";
		outSQL.InnerText = strSQL;   // and display it

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

		// create a new Command using the connection object and select statement
		OleDbCommand objCommand = new OleDbCommand(strSQL, objConnect);

		// declare an Integer variable to hold the number of records affected
		int intRowsAffected;

		try
		{
			// open the connection to the database
			objConnect.Open();

			// execute the SQL statement against the command
			// the ExecuteNonQuery method returns the number of records affected
			intRowsAffected = objCommand.ExecuteNonQuery();
		}
		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
		}

		// declare a string to display the results
		string strResult;

		// show the number of records affected
		strResult = "Executed SQL statement, <b>" + intRowsAffected.ToString()
				+ "</b> record(s) affected<br />Reading back from the database...<br />";

		// read the data back again - specify a SQL statement to
		// extract just this record for the existing Command object
		objCommand.CommandText = "SELECT * FROM BookList WHERE ISBN='1861003005'";

		try
		{
			// declare a variable to hold a DataReader object
			OleDbDataReader objDataReader;

			// execute the SQL statement against the command to fill the DataReader
			objDataReader = objCommand.ExecuteReader();

			// iterate through the records in the DataReader getting field values
			// the Read method returns False when there are no more records
			while (objDataReader.Read())
				strResult += "ISBN='<b>" + objDataReader["ISBN"] + "</b>' &nbsp; Title='<b>"
							+ objDataReader["Title"] + "'</b>";

			// close the DataReader and Connection
			objDataReader.Close();
			objConnect.Close();
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing updated data</b>.<br />"
						+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

		// display the result
		outResult.InnerHtml = strResult;

	}
</script>


<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="..\global\viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
+copy;2001 <a class="cite" href="http:// www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http:// www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>

⌨️ 快捷键说明

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