码迷,mamicode.com
首页 > 数据库 > 详细

ado.net 执行sql存储过程

时间:2015-11-07 00:57:15      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        int totalpage;
        List<StuInfo> List = new List<StuInfo>();
        gv_stu.DataSource = GetStuInfo(10, 2,  , out totalpage);
        gv_stu.DataBind();
    }
    public List<StuInfo> GetStuInfo(int pagesize,int currentpage,string condition,out int totalpage)
    {
        List<StuInfo> List = new List<StuInfo>();
        //string sqlconstr = "Data Source=.;Initial Catalog=Example;User ID=sa;Password=123456";
        string sqlconstr = "Data Source=.;Initial Catalog=Example;Integrated Security=true";
        string sql = "proc_GetStuInfo";
        SqlConnection con = new SqlConnection(sqlconstr);
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        SqlParameter[] para = {
                                  new SqlParameter("@pagesize",pagesize),
                                  new SqlParameter("@currentpage",currentpage),
                                  new SqlParameter("@condition",condition),
                                  new SqlParameter("@pagesize",SqlDbType.Int)
                              };
        para[3].Direction = ParameterDirection.Output;
        cmd.Parameters.AddRange(para);
        {
            while(sdr.Read())
            {
                StuInfo stu = new StuInfo();
                stu.Stuid = sdr["stuid"].ToString();
                stu.Stuname = sdr["stuname"].ToString();
                stu.Age = Convert.ToInt32(sdr["age"]);
                stu.Address = sdr["address"].ToString();
                List.Add(stu);
            }
        }
        con.Close();
        totalpage =Convert.ToInt32(para[3].Value);
        return List;
    }
}

 

ado.net 执行sql存储过程

标签:

原文地址:http://www.cnblogs.com/cylblogs/p/4944010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!