asp.net 中使用带参数的存储过程
.aspx.cs
.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class test_procedure_test_input : System.Web.UI.Page
{
public static string connStr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DpBind();
}
}
public void DpBind()
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select Country from Customers";
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds,"Customers");
DropDownList1.DataSource = ds.Tables["Customers"].DefaultView;
DropDownList1.DataTextField = "Country";
DropDownList1.DataValueField = "Country";
DropDownList1.DataBind();
cmd.Dispose();
conn.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string country = DropDownList1.SelectedValue;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Customers_Select2";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter("@Country",SqlDbType.VarChar,100);
para.Value = country;
para.Direction = ParameterDirection.Input;
cmd.Parameters.Add(para);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds,"Customers");
GridView1.DataSource = ds.Tables["Customers"].DefaultView;
GridView1.DataBind();
sda.Dispose();
cmd.Dispose();
conn.Close();
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class test_procedure_test_input : System.Web.UI.Page
{
public static string connStr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DpBind();
}
}
public void DpBind()
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select Country from Customers";
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds,"Customers");
DropDownList1.DataSource = ds.Tables["Customers"].DefaultView;
DropDownList1.DataTextField = "Country";
DropDownList1.DataValueField = "Country";
DropDownList1.DataBind();
cmd.Dispose();
conn.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string country = DropDownList1.SelectedValue;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Customers_Select2";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter("@Country",SqlDbType.VarChar,100);
para.Value = country;
para.Direction = ParameterDirection.Input;
cmd.Parameters.Add(para);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds,"Customers");
GridView1.DataSource = ds.Tables["Customers"].DefaultView;
GridView1.DataBind();
sda.Dispose();
cmd.Dispose();
conn.Close();
}
}
}
.aspx
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_input.aspx.cs" Inherits="test_procedure_test_input" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_input.aspx.cs" Inherits="test_procedure_test_input" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>