C# C语言中的异步存储过程调用#

C# C语言中的异步存储过程调用#,c#,asp.net,asp.net-mvc,stored-procedures,model-view-controller,C#,Asp.net,Asp.net Mvc,Stored Procedures,Model View Controller,我是C#的新手。我正在尝试在MVC ASP.net应用程序中进行异步存储过程调用 这就是我的代码的样子: public void RevalidateRule(int ruleKey) { var conStr = ConfigurationManager.ConnectionStrings["ValidationModuleEntities"].ConnectionString; using (var conn = new SqlConnection

我是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();
        }
    }
每当我按下视图中的“保存”按钮时,它都会被触发。然而,它似乎什么也没做


有人有想法吗?

请用代码编写try-catch。 如果在存储过程中出现任何错误,您将在何处获得异常

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)
{

}

所以您希望将值从视图传递到控制器并执行存储过程。我会尽力解释如何快速完成任务。 以下是一个例子:

控制器

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" />
}
所以,这样你:

  • 创建
    HelperClass
    以执行存储过程
  • 将所需值从
    视图传递到
    控制器
  • 执行存储过程

  • 您是否尝试过调试并查看发生了什么?这是保存单击时调用的方法吗?您是否确定调用了此方法?什么似乎什么都不做,方法还是过程?这根本不是异步的。。。此外,如果它没有做任何事情,可能是因为它根本没有被调用。。。您是否尝试在此处添加断点?如果可能,请提供一个断点。另外,还有一个提示:网上不需要问候和评论,比如提前感谢,所以这不是OP问题的答案,你可以在评论中提到这一点。
    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();
            }
        }
    }