sql-stored-proc.aspx
来自「Professional ASP.NET source code」· ASPX 代码 · 共 76 行
ASPX
76 行
<%@Page Language="C#"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Implicit Parameters in MS SQL Server</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Implicit Parameters in MS SQL Server</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"> </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.SQLConnectionString;
outConnect.InnerText = strConnect; // and display it
// create the SQL statement that will call the stored procedure
// and provide the single parameter for the ISBN to match
// note that this syntax only works with the 'SQL' objects
string strCommandText = "FindFromTitleAndDate '%Professional%', '02/01/2000'";
outCommandText.InnerText = strCommandText; // and display it
// create a new Connection object using the connection string
SqlConnection objConnect = new SqlConnection(strConnect);
// create a new Command using the CommandText and Connection object
SqlCommand objCommand = new SqlCommand(strCommandText, objConnect);
// declare a variable to hold a DataReader object
SqlDataReader 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 + -
显示快捷键?