【问题标题】:Asynchronous Stored Procedure call in C#C#中的异步存储过程调用
【发布时间】:2017-03-30 09:50:39
【问题描述】:

我是 C# 新手。我正在尝试在我的 MVC ASP.net 应用程序中进行异步存储过程调用。

这是我的代码的样子:

public void RevalidateRule(int ruleKey)
    { 
        var conStr = ConfigurationManager.ConnectionStrings["ValidationModuleEntities"].ConnectionString;

        using (var conn = new SqlConnection(conStr))
        {
            conn.Open();
            SqlCommand command = new SqlCommand("[dbo].[ReValidateQuery]", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ValidationRuleKey", ruleKey));
            command.CommandTimeout = 50;
            command.ExecuteNonQuery();
        }
    }

每当我在我的视图中按下保存按钮时都必须触发它。但是,它似乎没有做任何事情。

有人有想法吗?

【问题讨论】:

  • 你试过调试看看会发生什么吗?
  • 这是保存点击时调用的方法吗?
  • 你确定调用了这个方法吗?什么似乎什么都不做,方法还是过程?
  • 这根本不是异步的……另外,如果它什么也没做,可能是因为它根本没有被调用……你有没有尝试在那里添加断点?
  • 如果可能,请提供minimal reproducible example。另外,只是一个提示:SO 不需要像“提前致谢”这样的问候和 cmets

标签: c# asp.net asp.net-mvc stored-procedures model-view-controller


【解决方案1】:

所以你想将值从你的视图传递给控制器​​并执行存储过程。我将尝试解释如何快速完成任务。 示例如下:

控制器

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(string data)
{
    int ruleKey = Convert.ToInt32(data);
    Helper helper = new Helper();
    helper.RevalidateRule(ruleKey);
    return View();
}

查看

@using (Html.BeginForm())
{
    <input type="text" name="data" />
    <input type="submit" value="Submit" />
}

助手类

public class Helper
{
    public void RevalidateRule(int ruleKey)
    {
        var conStr = ConfigurationManager.ConnectionStrings["ValidationModuleEntities"].ConnectionString;

        using (var conn = new SqlConnection(conStr))
        {
            conn.Open();
            SqlCommand command = new SqlCommand("[dbo].[ReValidateQuery]", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ValidationRuleKey", ruleKey));
            command.CommandTimeout = 50;
            command.ExecuteNonQuery();
        }
    }
}

所以,这样你:

  1. 创建您的HelperClass 以执行您的存储过程
  2. 将所需值从您的View 传递给Controller
  3. 执行您的存储过程。

【讨论】:

    【解决方案2】:

    请在代码中编写try catch。 如果 Store 过程中出现任何问题,您将在哪里得到异常

    try
    {
      var conStr = ConfigurationManager.ConnectionStrings["ValidationModuleEntities"].ConnectionString;
    
            using (var conn = new SqlConnection(conStr))
            {
                conn.Open();
                SqlCommand command = new SqlCommand("[dbo].[ReValidateQuery]", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ValidationRuleKey", ruleKey));
                command.CommandTimeout = 50;
                command.ExecuteNonQuery();
            }
    }
    catch(exception ex)
    {
    
    }
    

    【讨论】:

    • 这不是 OP 问题的答案,您可以在评论中提到这一点。
    猜你喜欢
    • 2011-03-18
    • 2010-09-06
    • 2021-04-02
    • 2020-01-15
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多