parameter-stored-proc.aspx
来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 100 行
ASPX
100 行
<%@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>Using Explicit Parameters with a Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Explicit Parameters with a 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>Command Text: <b><span id="outCommandText" runat="server"></span></b></div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameters: <b><span id="outOutParams" runat="server"></span></b></div>
<div id="outError" 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 name of the stored procedure to use as the CommandText
string strCommandText = "FindFromISBN";
outCommandText.InnerText = strCommandText; // and display it
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// create a new Command object and set the CommandType
OleDbCommand objCommand = new OleDbCommand(strCommandText, 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 = "1861002882";
// create a new Parameter object named 'Title' with the correct data
// type to match a SQL database 'varchar' field of 50 characters
// and specify that it's an output parameter (so no value required)
objParam = objCommand.Parameters.Add("Title", OleDbType.VarChar, 50);
objParam.Direction = ParameterDirection.Output;
// create another output Parameter object named 'Date' with the correct
// data type to match a SQL database 'datetime' field
objParam = objCommand.Parameters.Add("Date", OleDbType.DBDate);
objParam.Direction = ParameterDirection.Output;
// display the value of the input parameter
outInParams.InnerText = "ISBN='" + objCommand.Parameters["ISBN"].Value + "'";
try
{
// open the connection and execute the command
objConnect.Open();
objCommand.ExecuteNonQuery();
objConnect.Close(); // then close the connection
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
}
// collect the values of the output parameters - note the use of
// the ToString method as they will contain DBNull if there was no
// match for the ISBN and this will cause an error if displayed
string strTitle = objCommand.Parameters["Title"].Value.ToString();
string strDate = objCommand.Parameters["Date"].Value.ToString();
outOutParams.InnerHtml = "Title='" + strTitle + "' Date=" + strDate;
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?