simple-stored-proc.aspx

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

ASPX
77
字号
<%@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>Executing a Simple Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Executing a Simple 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 id="outError" runat="server">&nbsp;</div>

<asp:datagrid id="dgrResult" runat="server" />

<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

		// create the SQL statement that will call the stored procedure
		string strCommandText = "GetBooks";
		outCommandText.InnerText = strCommandText; // and display it

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

		// create a new Command using the CommandText and Connection object
		OleDbCommand objCommand = new OleDbCommand(strCommandText, objConnect);

		// set the CommandType to // Stored Procedure// 
		objCommand.CommandType = CommandType.StoredProcedure;

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

		try
		{
			// open the connection and execute the command
			objConnect.Open();
			objDataReader = objCommand.ExecuteReader();
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
						+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

		// assign the DataReader object to the DataGrid control
		dgrResult.DataSource = objDataReader;
		dgrResult.DataBind();  // and bind (display) the data
		objConnect.Close();    // then close the connection

	}
</script>


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

⌨️ 快捷键说明

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