ADO.NET执行存储过程

例子一(获取存储过程的返回列表)

在SQL Server中定义一个存储过程(普通的连表查询)

create proc [dbo].[userinfo_userLogin_query]
as
	select a.*,b.UserName from T_UserInfo a join T_UserLogin b on a.LoginId=b.LoginId

go

在vs中调用这个存储过程

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string conStr = ConfigurationManager.ConnectionStrings["salesConStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);

            string sqlStr="userinfo_userLogin_query"; //userinfo_userLogin_query是存储过程名称
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            cmd.CommandType = CommandType.StoredProcedure; //指明我这里要执行的是存储过程
            conn.Open(); //打开与数据库的连接

            SqlDataReader reader= cmd.ExecuteReader();

            List<T_UserInfo> list = new List<T_UserInfo>();
            while (reader.Read())
            {
                T_UserInfo obj = new T_UserInfo()
                {
                    Name = reader["Name"].ToString(),
                    Age = Convert.ToInt32(reader["Age"]),
                    Reamarks = reader["Reamarks"].ToString()
                };
                list.Add(obj);                
            }
            return View();
        }
    }


例子二(获取存储过程的输出参数)

在SQL Server中定义一个存储过程(分页查询)

if exists(select * from sys.objects where name='Proc_Loction_GetPageList')
begin 
	drop proc Proc_Loction_GetPageList
end
go
create proc Proc_Loction_GetPageList(@currentPage int, @sizePage int,@total int out)
as
begin
	set @total=(select count(locId) from location)--得到数据总条数
	select top (@sizePage) * from (select row_number() over(order by locId) as rowNumber,* from location)
	as t where t.rowNumber>@sizePage*(@currentPage-1);
end

在vs中执行这个存储过程,并获取存储过程的输出参数

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string conStr = ConfigurationManager.ConnectionStrings["salesConStr"].ConnectionString;

            SqlParameter totalPara = new SqlParameter("@total", SqlDbType.Int); //用于接收存储过程的输出参数(获取数据的总条数)
            totalPara.Direction = ParameterDirection.Output; //声明这个参数是一个输出参数

            using (SqlConnection conn = new SqlConnection(conStr))
            {
                string sqlStr = "Proc_Loction_GetPageList"; //Proc_Loction_GetPageList是存储过程名称
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                cmd.CommandType = CommandType.StoredProcedure; //指明我这里要执行的是存储过程

                SqlParameter[] para=new SqlParameter[]
                {
                    new SqlParameter("@currentPage", 3),//当前页
                    new SqlParameter("@sizePage", 10) //页大小
                };

                cmd.Parameters.AddRange(para);  //将参数加入到cmd命令中             
                cmd.Parameters.Add(totalPara);

                conn.Open(); //打开与数据库的连接

                SqlDataReader reader = cmd.ExecuteReader();

                List<location> list = new List<location>();
                while (reader.Read()) //逐行读取数据
                {
                    location obj = new location()
                    {
                        locId = Convert.ToInt32(reader["locId"]),
                        locName = reader["locName"].ToString(),
                        parentId = Convert.ToInt32(reader["parentId"]),
                    };
                    list.Add(obj);
                }
            }
            var total = totalPara.Value; //获取输出参数(获取数据的总条数)

            return View();
        }
    }

例子三(获取存储过程的return返回值)

获取存储过程的return返回值

首先在SQL Server中定义一个存储过程

create proc ProcReturn(@number int)
as
begin
	if @number>5
		return 1
	else
		return 2
end

在vs中执行这个存储过程,并获取这个存储过程的return的返回值 (注意哦。我的存储过程中并没有@return这个参数的哦,但是我在c#代码中却使用了)

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string conStr = ConfigurationManager.ConnectionStrings["salesConStr"].ConnectionString;
     
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            string sqlStr = "ProcReturn"; //ProcReturn是存储过程名称
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            cmd.CommandType = CommandType.StoredProcedure; //指明我这里要执行的是存储过程

            
            //定义参数
            SqlParameter number = new SqlParameter("@number", 3);
            SqlParameter returnPara = new SqlParameter("@return", SqlDbType.Int); //用于接收存储过程的return返回值
            returnPara.Direction = ParameterDirection.ReturnValue; //声明这个参数是一个返回值


            cmd.Parameters.Add(number);
            cmd.Parameters.Add(returnPara);

            conn.Open(); //打开与数据库的连接

            cmd.ExecuteReader(); //用cmd.ExecuteNonQuery()也可以
            

            var result1=cmd.Parameters["@return"].Value.ToString(); //这样就可以获得存储过程的return返回值
            var result2 = returnPara.Value; //这样可以可以获得存储过程的返回值
          
        }
        

        return View();
    }
}

ADO.NET 断开式执行存储过程(使用DataAdapter)

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string conStr = ConfigurationManager.ConnectionStrings["salesConStr"].ConnectionString;
     
        
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            conn.Open();
            string sql = "Proc_Loction_GetPageList";//Proc_Loction_GetPageList是一个存储过程的名字
            SqlParameter[] ps = new SqlParameter[]
            {
                new SqlParameter("@currentPage", 1),
                new SqlParameter("@sizePage",10)
            };
            SqlParameter totalPar = new SqlParameter("@total", SqlDbType.Int);
            totalPar.Direction = ParameterDirection.Output; //声明这个参数是一个输出参数

            SqlDataAdapter da = new SqlDataAdapter(sql,conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure; //声明执行的是存储过程
            da.SelectCommand.Parameters.AddRange(ps);
            da.SelectCommand.Parameters.Add(totalPar);

            DataTable dt = new DataTable();
            da.Fill(dt);         
          
        }
        return View();
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值