[英]Run Stored Procedure and Check Value Exists or Not C#
我有这个存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ClientDelete
@clientid uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;
UPDATE Clients SET enabled=1,editDate=GETUTCDATE() WHERE clientid=@clientid
END
使用此功能文件:
Feature: ClientDelete
Scenario: Client Delete
Given a clean database
Given the following Clients table
| clientid | name | url | enabled | lastChangedBy | createDate | editDate |
| 1 | Client1 | https://test | true | test | 2000-01-01 | 2000-01-01 |
| 2 | Client2 | https://test | true | test | 2000-01-01 | 2000-01-01 |
When the "ClientDelete" stored procedure is run with the following parameters
| name | value |
| clientid | 11 |
Then the following is returned
| clientid | name | url | enabled | lastChangedBy | createDate | editDate |
| 1 | Client1 | https://test | false | test | 2000-01-01 | 2000-01-01 |
| 2 | Client2 | https://test | true | test | 2000-01-01 | 2000-01-01 |
C# 代码:
[When(@"the ""(.*)"" stored procedure is run with the following parameters")]
public void WhenTheStoredProcedureIsRunWithTheFollowingParameters(string procName, Table table)
{
using (var con = CreateDBConnection())
{
using (var cmd = con.CreateCommand())
{
cmd.CommandText = procName;
cmd.CommandType = CommandType.StoredProcedure;
foreach (var row in table.Rows)
{
var param = cmd.CreateParameter();
param.ParameterName = row.Values.First();
if (param.ParameterName == "clientids")
{
param.SqlDbType = SqlDbType.Structured;
param.Value = new List<SqlDataRecord>()
{
ToSqlDataRecord(new Guid(row.Values.Last()))
};
}
else if (param.ParameterName == "docTypes")
{
param.SqlDbType = SqlDbType.Structured;
if (row.Values.Last() != "NULL")
{
param.Value = new List<SqlDataRecord>()
{
ToSqlDataRecord(row.Values.Last())
};
}
}
else if (row.Values.Last() != "NULL")
param.Value = row.Values.Last();
else
param.Value = DBNull.Value;
cmd.Parameters.Add(param);
}
var results = new DataTable();
using (var adapter = new SqlDataAdapter((SqlCommand)cmd))
{
adapter.Fill(results);
}
_context.Add("Results", results);
}
}
}
[Then(@"the following is returned")]
public void ThenTheFollowingIsReturned(Table table)
{
var results = _context.Get<DataTable>("Results");
Assert.AreEqual(table.Rows.Count, results.Rows.Count);
for (int i = 0; i < results.Rows.Count; i++)
{
var expectedRow = table.Rows[i];
var actualRow = results.Rows[i];
for (int j = 0; j < table.Header.Count; j++)
{
var name = table.Header.ElementAt(j);
var type = actualRow[name].GetType();
object expectedValue = expectedRow[name];
if (expectedValue.ToString().IsNullText())
expectedValue = null;
else if (type != typeof(DBNull))
expectedValue = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(expectedRow[name]);
var actualValue = Convert.IsDBNull(actualRow[name]) ? null : actualRow[name];
Assert.AreEqual(expectedValue, actualValue, name);
}
}
}
@Then 不起作用,因为它没有从存储过程中返回任何内容,所以我想检查值是否已更新或是否存在。 我也有这个,然后用于返回行数据并且工作正常的多个存储过程,但不适用于添加、删除、更新。
注意:我不需要在存储过程中添加 output 参数。
在存储过程中更新后添加:
SELECT @@ROWCOUNT AS Result
这将返回受前一个更新语句影响的行数。 然后,如果结果 > 0,您可以检查您的 C# 代码。
@@ROWCOUNT 文档: https://docs.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql?view=sql-server-ver15
您需要在Then
步骤中再次查询 clients 表。 DeleteClient
存储过程不返回任何内容,也不填充 output 变量。 您唯一的办法是再次查询客户表, Then the following is returned
。 您可以使用 Table 扩展方法CompareToSet(...)而不是自己手动进行断言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.