简体   繁体   English

我可以在 ASP.NET 中显示存储过程的结果吗?

[英]Can I display the result of stored procedure in ASP.NET?

Here's my stored procedure, in which I'm inserting records to a database and, if a record exists, it gets updated.这是我的存储过程,我将记录插入到数据库中,如果记录存在,它会被更新。 Can I get result back, like how many records are updated and how many records are inserted?我可以取回结果,例如更新了多少条记录以及插入了多少条记录?

// stored procedure

ALTER PROCEDURE [dbo].[Update_F3_BC_Column_Mapping]
@tblF3_BC_Column_Mapping F3_BC_Column_MappingType READONLY
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO F3_BC_Column_Mapping c1
USING @tblF3_BC_Column_Mapping c2
ON c1.Id=c2.Id
WHEN MATCHED THEN
UPDATE SET c1.Source = c2.Source
      ,c1.Destination = c2.Destination
WHEN NOT MATCHED THEN
INSERT VALUES(c2.Id, c2.Source, c2.Destination);
END


//c# code

connection();
{
    using (SqlCommand cmd = new SqlCommand("Update_F3_BC_Column_Mapping"))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@tblF3_BC_Column_Mapping", dtMap);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

To get 2 different numbers, inserted and updated, you can alter your stored procedure to allow a couple of output variables, and handle them as parameters in c#.要获得 2 个不同的数字,插入和更新,您可以更改存储过程以允许几个输出变量,并在 c# 中将它们作为参数处理。

In the header of your procedure, add 2 output parameters:在过程的标题中,添加 2 个输出参数:

ALTER PROCEDURE [dbo].[Update_F3_BC_Column_Mapping]
@tblF3_BC_Column_Mapping F3_BC_Column_MappingType READONLY,
@updated_count int output,
@inserted_count int output
AS

In the body, declare a table variable to hold the output from the merge;在主体中,声明一个表变量来保存合并的输出;

BEGIN
    SET NOCOUNT ON;
    declare @outputtable table ([action] nvarchar(10), [count] int);

And then add a (last) line to the merge to fill this table:然后在合并中添加(最后)一行以填充此表:

    MERGE INTO F3_BC_Column_Mapping c1
    USING @tblF3_BC_Column_Mapping c2
        ON c1.Id=c2.Id
    WHEN MATCHED THEN
        UPDATE SET c1.Source = c2.Source
              ,c1.Destination = c2.Destination
    WHEN NOT MATCHED THEN
        INSERT VALUES(c2.Id, c2.Source, c2.Destination)
    OUTPUT $action , 1   INTO @outputtable;

Now you can populate your output variables by counting rows in this output table to end your stored procedure:现在您可以通过计算此输出表中的行数来填充您的输出变量以结束您的存储过程:

    select @updated_count = sum([count]) from @outputtable where [action] = 'UPDATE';
    select @inserted_count = sum([count]) from @outputtable where [action] = 'INSERT';
END

(All of the code rows posted so far make up your new stored procedure.) (到目前为止发布的所有代码行构成了您的新存储过程。)

You now need to add 2 additional parameters to you c# code:您现在需要向您的 c# 代码添加 2 个附加参数:

connection();
{
    using (SqlCommand cmd = new SqlCommand("Update_F3_BC_Column_Mapping"))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@tblF3_BC_Column_Mapping", dtMap);

        cmd.Parameters.Add("@updated_count", SqlDbType.Int).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@inserted_count", SqlDbType.Int).Direction = ParameterDirection.Output;

And after you have run your query, you can access the outputs by using the value property:运行查询后,您可以使用 value 属性访问输出:

        con.Open();
        cmd.ExecuteNonQuery();

        int updated = Convert.ToInt32(cmd.Parameters["@updated_count"].Value);
        int inserted = Convert.ToInt32(cmd.Parameters["@inserted_count"].Value);

and then finish off your code:然后完成你的代码:

        con.Close();
    }
}

Based on MSDN :基于MSDN

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.对于 UPDATE、INSERT 和 DELETE 语句,返回值是受命令影响的行数。 When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers.当插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受触发器影响的行数。 For all other types of statements, the return value is -1.对于所有其他类型的语句,返回值为 -1。 If a rollback occurs, the return value is also -1.如果发生回滚,则返回值也是 -1。

So you just need to store the ExecuteNonQuery 's return value in a variable like this:所以你只需要将ExecuteNonQuery的返回值存储在这样的变量中:

int count = cmd.ExecuteNonQuery();

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

相关问题 ASP.Net MVC显示存储过程的结果 - ASP.Net MVC display result from stored procedure 在 ASP.NET MVC 中显示存储过程选择结果 - Display stored procedure select results in ASP.NET MVC 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC? 如何使用EntityFramework 7和Asp.Net 5调用SQL存储过程 - How can I call a SQL Stored Procedure using EntityFramework 7 and Asp.Net 5 如何在asp.net中将存储过程的结果转换为逗号分隔的列表? - How can I turn the results of a stored procedure into a comma-separated list in asp.net? 如何在Linq-to-SQL和asp.net中使用存储过程? - How can I use a stored procedure in Linq-to-SQL and asp.net? 我可以使用带有实体框架(asp.net mvc)的另一台服务器上的一台服务器上的存储过程吗? - Can I use a stored procedure from one server on another with entity framework (asp.net mvc)? 如何存储将行或列值返回到ASP.NET C#变量的SQL存储过程结果? - How do I store a SQL stored procedure result that returns a row or a column value into an ASP.NET C# variable? 在asp.net中使用存储过程进行自定义分页 - Custom pagination with stored procedure in asp.net Ajax自动完成存储过程ASP.NET - Ajax Autocomplete Stored Procedure ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM