简体   繁体   English

无法从MVC执行Sql Server存储过程

[英]Sql Server Stored Procedure not executing from MVC

I am trying to execute a stored procedure in my Sql Server Database from an Asp.Net MVC project. 我正在尝试从Asp.Net MVC项目在我的Sql Server数据库中执行存储过程。 I have set it up in a way that I can use it for testing purposes, which is why the variable "procedureTest" is a constant value (I know that "2044" is an existing record in my database). 我已经将其设置为可以用于测试目的的方式,这就是为什么变量“ procedureTest”是常量值的原因(我知道“ 2044”是数据库中的现有记录)。 I will change this once I accomplish a successful run. 成功运行后,我将更改此设置。 Also, I know that my stored procedure works because I have executed it in Sql Server Management Studio successfully. 另外,我知道我的存储过程可以正常运行,因为我已经在Sql Server Management Studio中成功执行了它。 The procedure has the task of adding ID from one table to another, but I have yet to see it appear in this table. 该过程的任务是将ID从一个表添加到另一个表,但是我还没有看到它出现在该表中。 I am not receiving an error in my catch block so I am kind of lost at the moment. 我没有在catch块中收到错误,因此我现在有点迷路了。 I could definitely use your help. 我绝对可以使用您的帮助。

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);
} 

Stored Procedure: 存储过程:

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

Change you code like this 像这样更改您的代码

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();
        }

First of all, you missed to specify the command type. 首先,您没有指定命令类型。 Also using EXEC in SqlCommand is not a proper way. 在SqlCommand中使用EXEC也不是正确的方法。

Please try with the below code 请尝试以下代码

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()
            }
        }

Since you are calling the sp inside a while loop, wrap the code in using() { } to automatically dispose the command object after each iteration 由于您是在while循环中调用sp,因此将代码包装在using() { }以便在每次迭代后自动处理命令对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM