transacted-storedproc.aspx

来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 103 行

ASPX
103
字号
<%@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 Transacted Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Updating Data with a Transacted Stored Procedure</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>Stored Procedure Name: <b><span id="outSQL" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameter: <b><span id="outOutParams" runat="server"></span></b></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 stored procedure name
		string strSQL = "DoBookArchive";
		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 stored proc name
		OleDbCommand objCommand = new OleDbCommand(strSQL, objConnect);
		objCommand.CommandType = CommandType.StoredProcedure;

		// create a variable to hold a Parameter object
		OleDbParameter objParam;

		// create a new Parameter object named // ISBN' with the correct data
		// type to match a SQL database // varchar' field of 12 characters
		objParam = objCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12);

		// specify that it// s an Input parameter and set the value
		objParam.Direction = ParameterDirection.Input;
		objParam.Value = "1999999999";

		// create an output Parameter object named // Result' with the correct
		// data type to match a SQL database // integer' field
		// specify that it// s an Output parameter so no value required
		objParam = objCommand.Parameters.Add("Result", OleDbType.Integer);
		objParam.Direction = ParameterDirection.Output;

		// display the value of the input parameter
		outInParams.InnerText = "ISBN='" + objCommand.Parameters["ISBN"].Value + "'";

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

			// execute the SQL statement against the command
			objCommand.ExecuteNonQuery();
			objConnect.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
		}

		// collect and display the value of the output parameter
		int intResult = Convert.ToInt32(objCommand.Parameters["Result"].Value);
		string strResult = "Result='" + intResult.ToString() + "'<br />";

		switch (intResult)
		{
		case -1: strResult += "Error occurred while attempting archive"; break;
		case 0: strResult += "Failed to archive book - no matching book found"; break;
		default: strResult += "Successfully archived the specified book"; break;
		}
		
		outOutParams.InnerHtml = strResult;

	}
</script>

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

⌨️ 快捷键说明

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