ASP.NET实现页面间值传递的几种方法

首先,打开你的查询分析器。。

运行下列SQL代码。

CREATE PROCEDURE dbo.oa_selectalluser
AS
    select * from zz_Stock_Barcode 这个表可以改成对应的测试表,下面的也一样
GO


CREATE PROCEDURE dbo.oa_SelectByID
    @id int
AS
    select * from zz_Stock_Barcode where ID=@id
GO

命令运行成功后你会看到存储过程中多了两条记录dbo.oa_selectalluser 和dbo.oa_SelectByID

下面开始写操作aspx页面

1。从工具箱中拖出一个TextBox,ID为:TextBox1(一般不用改,系统为默认为此ID)

2。再拖一个Button,ID为:Button1

3。再拖一个GridView,ID为:GridView1,(用来显示数据的)

好了,双击Button控件,在事件中添加代码,(代码如下:)

view plaincopy to clipboardprint?
protected void Button1_Click(object sender, EventArgs e)  
    {  
        //带参数的存储过程的使用方法  
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);  
        SqlDataAdapter da = new SqlDataAdapter();  
        DataSet ds = new DataSet();  
        da.SelectCommand = new SqlCommand();  
        da.SelectCommand.Connection = conn;  
        da.SelectCommand.CommandText = "oa_SelectByID";  
        da.SelectCommand.CommandType = CommandType.StoredProcedure;  

        SqlParameter param = new SqlParameter("@id", SqlDbType.Int);  
        param.Direction = ParameterDirection.Input;  
        param.Value = Convert.ToInt32(TextBox1.Text);  
        da.SelectCommand.Parameters.Add(param);  

        da.Fill(ds);  
        GridView1.DataSource = ds;  
        GridView1.DataBind();  
    }
protected void Button1_Click(object sender, EventArgs e)
    {
        //带参数的存储过程的使用方法
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = new SqlCommand();
        da.SelectCommand.Connection = conn;
        da.SelectCommand.CommandText = "oa_SelectByID";
        da.SelectCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter param = new SqlParameter("@id", SqlDbType.Int);
        param.Direction = ParameterDirection.Input;
        param.Value = Convert.ToInt32(TextBox1.Text);
        da.SelectCommand.Parameters.Add(param);

        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

再在Page_Load中添加代码,(代码如下:)

view plaincopy to clipboardprint?
protected void Page_Load(object sender, EventArgs e)  
   {  
       if (!Page.IsPostBack)  
       {  
           //不带参数的存储过程的使用方法  
           SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);  
           SqlDataAdapter da = new SqlDataAdapter();  
           DataSet ds = new DataSet();  
           da.SelectCommand = new SqlCommand();  
           da.SelectCommand.Connection = conn;  
           da.SelectCommand.CommandText = "oa_SelectAllUser";  
           da.SelectCommand.CommandType = CommandType.StoredProcedure;  
           da.Fill(ds);  
           GridView1.DataSource = ds;  
           GridView1.DataBind();  
       }  
   }
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //不带参数的存储过程的使用方法
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = new SqlCommand();
            da.SelectCommand.Connection = conn;
            da.SelectCommand.CommandText = "oa_SelectAllUser";
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

运行,OK。。

网上有很多asp.net存储过程入门的文章,包括在论坛提问的,都没太写清楚过程。让新手一头雾水,我当初就是这样。希望此文能对各位“菜鸟”有所帮助,希望大家早日飞黄腾达。^_^

 

posted @ 2010-09-17 07:37  英雄不问出处  阅读(231)  评论(0编辑  收藏  举报