现在的位置: 首页 > 综合 > 正文

举例 .net中使用 不带or带参数的存储过程

2012年07月24日 ⁄ 综合 ⁄ 共 1852字 ⁄ 字号 评论关闭

(1) 不带参数的存储过程

CREATE PROCEDURE dbo.oa_selectalluser 
AS
    select * from UserInfo
GO

(2) 带参数的存储过程
CREATE PROCEDURE dbo.oa_SelectByID
    @id int
AS
    select * from UserInfo where ID=@id
GO

下面介绍怎么在VS2005中使用这两个存储过程.
(一).不带参数的存储过程:

protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack)
        {
           //不带参数的存储过程的使用方法
           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());
           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();
        }
在页面中添加了一个GridView控件用来绑定执行存储过程得到的结

(二).带参数的存储过程:

protected void btn_search_Click(object sender, EventArgs e)
    {
        //带参数的存储过程的使用方法
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());
        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(txt_value.Text);
        da.SelectCommand.Parameters.Add(param);

        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
同样,在页面中添加了一个GridView控件用来绑定执行存储过程的结果,另外,在页面中还添加了一个textbox控件和一个BUTTON按钮,上面的执行存储过程是放在按钮的onclick事件中的.textbox控件用来接收存储过程的参数.

抱歉!评论已关闭.