ado.net 执行sql存储过程
时间:2021-07-01 10:21:17
帮助过:3人阅读
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存储过程
标签: