asp.net中使用带输入输出参数的存储过程

以下为用到的存储过程:

 

存储过程

 

.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();
        }
    }
}

 

.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_output.aspx.cs" Inherits="test_procedure_test_output" %>

<!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>
    
    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        
<asp:DropDownList ID="DropDownList1" runat="server" 
            onselectedindexchanged
="DropDownList1_SelectedIndexChanged" 
            AutoPostBack
="True">
        
</asp:DropDownList>
        
<br />
        
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        
<asp:GridView ID="GridView1" runat="server">
        
</asp:GridView>
    
        
<br />
        
<br />
        
<asp:Label ID="Label1" runat="server"></asp:Label>
    
    
</div>
    
</form>
</body>
</html>

posted on 2008-11-02 13:44  鱼跃于渊  阅读(254)  评论(0编辑  收藏  举报

导航