zoukankan      html  css  js  c++  java
  • ado.net 执行sql存储过程

    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;
        }
    }
  • 相关阅读:
    Git使用经验小结
    Git使用经验小结
    关于IT增值服务"拜师学艺"价格调整的通知
    关于IT增值服务"拜师学艺"价格调整的通知
    Java实现 LeetCode 397 整数替换
    Java实现 LeetCode 397 整数替换
    Java实现 LeetCode 397 整数替换
    Java实现 LeetCode 396 旋转函数
    Java实现 LeetCode 396 旋转函数
    Java实现 LeetCode 396 旋转函数
  • 原文地址:https://www.cnblogs.com/cylblogs/p/4944010.html
Copyright © 2011-2022 走看看