【问题标题】:Sql Server Stored Procedure not executing from MVCSql Server 存储过程未从 MVC 执行
【发布时间】:2015-07-21 05:08:27
【问题描述】:

我正在尝试通过 Asp.Net MVC 项目在我的 Sql Server 数据库中执行存储过程。我已将其设置为可以将其用于测试目的,这就是为什么变量“procedureTest”是一个常量值的原因(我知道“2044”是我数据库中的现有记录)。一旦成功运行,我将更改此设置。另外,我知道我的存储过程有效,因为我已经在 Sql Server Management Studio 中成功执行了它。该过程的任务是将 ID 从一个表添加到另一个表,但我还没有看到它出现在这个表中。我的 catch 块中没有收到错误,所以我现在有点迷路了。我绝对可以使用你的帮助。

try
{
    using (var connection = new SqlConnection(connectionString))
    {
        connection.Open();

        int procedureTest = 2044;

        var command = new SqlCommand("SELECT ID FROM Images WHERE ID = @id", connection);
        var paramDate = new SqlParameter("@id", procedureTest);
        command.Parameters.Add(paramDate);
        var reader = command.ExecuteReader();

        while (reader.Read())
        {
            var storedProcCommand = new SqlCommand("EXEC addToNotificationTable @ID", connection);
            var paramId = new SqlParameter("@ID", reader.GetInt32(0));
            storedProcCommand.Parameters.Add(paramId);
            command.ExecuteNonQuery();
        }
    }
}
catch (Exception e)
{
    string exceptionCause = String.Format("An error occurred: '{0}'", e);
    System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\exception.txt", exceptionCause);
} 

存储过程:

CREATE PROCEDURE addToNotificationTable @ID int
AS
Insert NotificationTable (ID)
SELECT ID 
FROM Images
Where ID = @ID

【问题讨论】:

  • 您的SELECT 命令末尾没有;...
  • 1.发布存储过程。 2. 如果您按 F12 并进入网络选项卡,您是否看到预期的数据按预期传递到您的控制器?你怎么称呼这个? 3. 使用 SQL Profiler 监控数据库并确保正在调用存储的过程。这段代码在进入数据库之前可能会出现很多问题。
  • @Nick.McDermaid,刚刚添加了程序。最初,我将它放在一个在 Global.asax.cs 中初始化的计时器中,但不确定是计时器还是此代码导致它无法工作,因此我决定从从接收数据的操作结果中调用它不同的应用程序,然后将数据添加到我的数据库中。我只是将它添加到我的操作结果的底部,因为我知道操作结果很好。这是一个快速的尝试,只是想看看我是否可以让它工作,然后从那里开始。我会尝试你所说的#2和#3。
  • @Ben,你的意思是我需要一个 ;在 SELECT 命令查询的引号内?
  • 您似乎在第二条命令中缺少 CommandType,请尝试使用: var storedProcCommand = new SqlCommand("EXEC addToNotificationTable @ID", connection) {CommandType= CommandType.StoredProcedure };

标签: c# sql-server asp.net-mvc stored-procedures


【解决方案1】:

像这样改变你的代码

while (reader.Read())
        {
            var storedProcCommand = new SqlCommand("EXEC addToNotificationTable @ID", connection);
            var paramId = new SqlParameter("@ID", reader.GetInt32(0));
            storedProcCommand.Parameters.Add(paramId);
            storedProcCommand.ExecuteNonQuery();
        }

【讨论】:

    【解决方案2】:

    首先,您错过了指定命令类型。在 SqlCommand 中使用 EXEC 也不是正确的方法。

    请尝试以下代码

    while (reader.Read())
            {
                using(SqlCommand storedProcCommand = new SqlCommand("addToNotificationTable", connection)) //Specify only the SP name
                { 
                    storedProcCommand.CommandType = CommandType.StoredProcedure; //Indicates that Command to be executed is a stored procedure no a query
                    var paramId = new SqlParameter("@ID", reader.GetInt32(0));
                    storedProcCommand.Parameters.Add(paramId);
                    storedProcCommand.ExecuteNonQuery()
                }
            }
    

    由于您在 while 循环内调用 sp,因此将代码包装在 using() { } 中以在每次迭代后自动处理命令对象

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 2019-02-05
      • 1970-01-01
      • 2015-08-28
      • 2019-04-03
      • 2015-06-18
      • 2010-10-29
      • 2010-12-11
      • 1970-01-01
      相关资源
      最近更新 更多