asp.net mvc 调用sql中的存储过程的误解

我要在一张“DateType”表中插入一年的日期和日期分为“工作日”和“周六日”的字段。在sql中的存储过程是这样写的:

CREATE PROCEDURE [dbo].[DateType_INSERT_DATA]
AS
delete from DateType where 1=1
declare @i datetime
set @i= dateadd(d,1-datepart(dy,getdate()),getdate())
declare @year int
set @year=datepart(yy,@i)
while(@year=datepart(yy,@i))
begin
insert into DateType values(@i,1)
set @i=dateadd(d,1,@i)
end

declare @rel int,@reltime datetime
declare cursor_datetime cursor for
select Datetimes from DateType;
open cursor_datetime
fetch next from cursor_datetime into @reltime
while @@FETCH_STATUS=0
begin
---cast('YYYY-MM-DD HH24:MI:SS' as datetime)
select @rel=datepart(weekday,@reltime)
if @rel=1
begin
update DateType set DateType=2 where Datetimes=@reltime
end
fetch next from cursor_datetime into @reltime
end
close cursor_datetime
deallocate cursor_datetime;


--declare @rel int,@reltime datetime
declare cursor_datetime cursor for
select Datetimes from DateType;
open cursor_datetime
fetch next from cursor_datetime into @reltime
while @@FETCH_STATUS=0
begin
---cast('YYYY-MM-DD HH24:MI:SS' as datetime)
select @rel=datepart(weekday,@reltime)
if @rel=7
begin
update DateType set DateType=2 where Datetimes=@reltime
end
fetch next from cursor_datetime into @reltime
end
close cursor_datetime
deallocate cursor_datetime;

 

我要在项目的控制器的方法中执行DateType_INSERT_DATA存储过程。在网上查了很多方法都报错,最后发现原来很简单的一句话就会成功,即:

public string NewYearDateType()
{
try {
new EmAttContext().Database.ExecuteSqlCommand("exec DateType_INSERT_DATA");
return "success";
}
catch (Exception ex) {
return ex.ToString();
}
}

转载于:https://www.cnblogs.com/tiecaiying/p/8328193.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值