使用存储过程从web页面传递参数
首先创建存储过程,格式如下:CREAT PROCEDURE sp_CustomersByState @region nvarchar(15)
AS
select CustomerID,CompanyName from Customers
where region=@region Order by CompanyName
RETURN
编写程序代码:
在C#代码中,我们将使用新的类,System.Data.SqlClient.Parameter。该类的对象设计用于表示存储过程中的参数,因此构造函数需要知道名称、数据类型和所讨论的参数的大小。
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head><title>Using Stored Procedures With Parameters</title></head>
<body>
<form runat="server" method="post">
Enter a State Code:
<asp:Textbox id="txtRegion" runat="server" />
<asp:Button id="btnSubmit" runat="server"
Text="Search" OnClick="Submit" />
<br/><br/>
<asp:DataGrid id="dgOutput" runat="server" />
</form>
</body>
</html>
<script language="c#" runat="server">
private void Submit(object sender, EventArgs e)
{
String strConnection ="Server=224NUMECA;database=Northwind;user id=sa;password=sa";
SqlConnection objConnection = new SqlConnection(strConnection);
补充:asp教程,高级应用