Ado调用存储过程

  public ReturnModel GetStudent(string json)
        {
            ParamsModel model = JsonConvert.DeserializeObject<ParamsModel>(json);

            //连接对象
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["StudentConn"].ToString()))
            {
                //命令对象
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;//存储过程类型
                cmd.CommandText = "proc_Page";//存储过程名称
                //参数  ParameterName参数名字   SqlDbType参数类型   SqlValue参数值  Direction参数描述(输出、输入)
                SqlParameter[] sqls = new SqlParameter[] {
                    new SqlParameter{ ParameterName="BigScore", SqlDbType=SqlDbType.Int,SqlValue=model.BigScore },
                    new SqlParameter{ ParameterName="CID", SqlDbType=SqlDbType.Int,SqlValue=model.CID },
                    new SqlParameter{ ParameterName="ClaId", SqlDbType=SqlDbType.Int,SqlValue=model.ClaId },
                    new SqlParameter{ ParameterName="order", SqlDbType=SqlDbType.Int,SqlValue=model.order },
                    new SqlParameter{ ParameterName="PageIndex", SqlDbType=SqlDbType.Int,SqlValue=model.PageIndex },
                    new SqlParameter{ ParameterName="PageSize", SqlDbType=SqlDbType.Int,SqlValue=model.PageSize },
                    new SqlParameter{ ParameterName="Sage", SqlDbType=SqlDbType.VarChar,SqlValue=model.Sage },
                    new SqlParameter{ ParameterName="SmallScore", SqlDbType=SqlDbType.Int,SqlValue=model.SmallScore },
                    new SqlParameter{ ParameterName="Sname", SqlDbType=SqlDbType.VarChar,Size=20,SqlValue=model.Sname },
                    new SqlParameter{ ParameterName="Tname", SqlDbType=SqlDbType.VarChar,Size=20,SqlValue=model.Tname },
                    new SqlParameter{ ParameterName="TotalCount", SqlDbType=SqlDbType.Int, Direction=ParameterDirection.Output},
                };
                cmd.Parameters.AddRange(sqls);
                //适配器
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("Student");
                sda.Fill(dt);
                //总条数
                int totalCount = Convert.ToInt32(cmd.Parameters["TotalCount"].Value);

                ReturnModel returnModel = new ReturnModel
                {
                    Dt = dt,
                    TotalCount= totalCount
                };
              return returnModel;
            }
        }

 

posted @ 2020-02-18 07:48  噜啦啦0001  阅读(214)  评论(0编辑  收藏  举报