当前位置:   article > 正文

C#存储过程的调用和数据库中编写_c# 执行sql存储过程

c# 执行sql存储过程

1 项目中调用存储过程

  1. /// <summary>
  2. /// 数据库连接字符串
  3. /// </summary>
  4. public static string sqlconnectionString = ConfigurationManager.ConnectionStrings["sqlconn"].ToString();
  5. /// <summary>
  6. /// 数据库连接
  7. /// </summary>
  8. /// <returns></returns>
  9. public static SqlConnection SqlConnection()
  10. {
  11. var connection = new SqlConnection(sqlconnectionString);
  12. connection.Open();
  13. return connection;
  14. }
  15. //方法
  16. public int upOrderInfo(QX_ScoreOrderInfoModel OrdModel)
  17. {
  18. int res = 0;
  19. try
  20. {
  21. using (var conn = SqlHelper.SqlConnection())
  22. {
  23. SqlCommand com = new SqlCommand();
  24. com.CommandText = "upOrderInfo";
  25. com.CommandType = CommandType.StoredProcedure;
  26. com.Connection = conn;
  27. com.Parameters.Add(new SqlParameter("@PayTypeID", OrdModel.PayTypeID));
  28. com.Parameters.Add(new SqlParameter("@PayTypeName", OrdModel.PayTypeName));
  29. com.Parameters.Add(new SqlParameter("@PayNO", OrdModel.PayNO));
  30. com.Parameters.Add(new SqlParameter("@PayAmount", OrdModel.PayAmount));
  31. com.Parameters.Add(new SqlParameter("@OrderNO", OrdModel.OrderNO));
  32. //返回查询结果的第一行第一列的值
  33. res = com.ExecuteScalar().ToInt();
  34. }
  35. }
  36. catch (Exception e)
  37. {
  38. res = 0;
  39. }
  40. return res;
  41. }

 2 数据库中存储过程的编写

  1. USE [quanxi_tong]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[upOrderInfo] Script Date: 2022/11/9 11:07:29 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author: <Author,,Name>
  10. -- Create date: <Create Date,,>
  11. -- Description: <Description,,>
  12. -- =============================================
  13. ALTER PROCEDURE [dbo].[upOrderInfo]
  14. -- Add the parameters for the stored procedure here
  15. @PayTypeID int,
  16. @PayTypeName varchar(30),
  17. @PayNO varchar(50),
  18. @PayAmount decimal(14, 2),
  19. @OrderNO varchar(30)
  20. AS
  21. BEGIN
  22. --根据订单号查询订单信息
  23. declare @OrderType int,@Score int,@UserType int,@UserId int,@PayScore int,@ProductId int,@UseDay int
  24. select @OrderType=OrderType,@Score=Score,@UserType=UserType,@UserId=UserId,@PayScore=PayScore,@ProductId=ProductId,@UseDay=UseDay from QX_OrderInfo where OrderNO=@OrderNO;
  25. --获取机构积分信息
  26. declare @OrgScore int=0 ,@UserScore int=0
  27. if @UserType=1
  28. begin
  29. select @OrgScore=Score from QX_Base where BaseId=@UserId;
  30. end
  31. --获取用户积分信息
  32. if @UserType=0
  33. begin
  34. select @UserScore=Score from QX_Member where UserId=@UserId;
  35. end
  36. --开始执行修改
  37. begin tran tran1
  38. --1 修改订单信息表数据
  39. update QX_OrderInfo set PayTypeID=@PayTypeID,
  40. PayTypeName=@PayTypeName,
  41. PayNO=@PayNO,
  42. PayAmount=@PayAmount,
  43. PayTime=GETDATE(),
  44. PayStatus=2,
  45. Status=1,
  46. SuccessTime=GETDATE()
  47. where OrderNO=@OrderNO
  48. --2 判断购买类型
  49. --购买积分 OrderType=73 并且是机构会员
  50. if @@error =0
  51. if (@OrderType=73 and @UserType=1)
  52. begin
  53. --1 修改机构的积分信息
  54. update QX_Base set Score=Score+@Score where BaseId=@UserId;
  55. --2 去修改机构积分流水表
  56. insert into QX_ScoreOrg_TradeDetails (BaseId,BeforeScore,TradeScore,AfterScore,TradeType,TradeTime,TradeTitle,TradeDescription,SourceSystemCode,OrderNO)
  57. values (@UserId,@OrgScore,@Score,@OrgScore+@Score,0,GETDATE(),'购买积分','机构会员在机构中心上购买了积分套餐',1014,@OrderNO)
  58. end
  59. --购买全媒体 OrderType=74 并且用户支付积分大于0
  60. if (@OrderType=74 and @PayScore>0)
  61. begin
  62. --1 修改用户的积分信息
  63. update QX_Member set Score=Score-@PayScore where UserId=@UserId;
  64. --2 修改用户积分流水表
  65. insert into QX_Score_TradeDetails (UserId,BeforeScore,TradeScore,AfterScore,TradeType,TradeTime,TradeTitle,TradeDescription,SourceSystemCode,OrderNO)
  66. values (@UserId,@UserScore,@PayScore,@UserScore-@PayScore,1,GETDATE(),'购买消费积分','用户在用户中心购买商品消耗积分',1013,@OrderNO)
  67. end
  68. --购买会员 OrderType=75
  69. if @OrderType=75
  70. begin
  71. --1 根据产品id,获取角色id
  72. declare @RoleId int,@QX_UserRoleRelationId int=0,@EndTime datetime
  73. select @RoleId=RoleId from QX_Product where ProductId=@ProductId;
  74. if @RoleId>0
  75. --2 去用户角色权限关系表中添加,之前存在就修改
  76. select @QX_UserRoleRelationId=QX_UserRoleRelationId,@EndTime=EndTime from QX_UserRoleRelation where RoleId=@RoleId and UserId=@UserId;
  77. --没有记录,就新增数据
  78. if @QX_UserRoleRelationId =0
  79. insert into QX_UserRoleRelation (UserId,EndTime,CreateTime,RoleId) values (@UserId,dateadd(day,@UseDay,GETDATE()),GETDATE(),@RoleId)
  80. --存在就修改数据
  81. if @QX_UserRoleRelationId>0
  82. update QX_UserRoleRelation set EndTime=dateadd(day,@UseDay,@EndTime) where UserId=@UserId and RoleId=@RoleId;
  83. end
  84. if( @@error=0)
  85. begin
  86. commit tran tran1
  87. select '1' as result
  88. end
  89. else
  90. begin
  91. select '0' as result
  92. rollback tran tran1
  93. return;
  94. end
  95. END

3 项目中执行存储过程,根据返回值,做后续处理。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/480818
推荐阅读
相关标签
  

闽ICP备14008679号