[英]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();
}
}
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.