sql—运行存储过程并检查值是否存在c#

j7dteeu8  于 2021-07-27  发布在  Java
关注(0)|答案(2)|浏览(405)

我有一个存储过程:

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

@因为它没有从存储过程返回任何内容,所以我想检查值是否更新或存在。我也有这个,然后用于多个存储过程,它返回行数据,工作正常,但不适用于add、delete、update。
注意:我不需要在存储过程中添加输出参数。

esyap4oy

esyap4oy1#

在存储过程中更新后,添加:

SELECT @@ROWCOUNT AS Result

这将返回受上一个update语句影响的行数。如果结果大于0,则可以签入c代码。
@@行计数文档:https://docs.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql?view=sql-server-ver15

628mspwn

628mspwn2#

您需要在您的数据库中再次查询clients表 Then 步骤。这个 DeleteClient 存储过程不返回任何内容,也不填充输出变量。您唯一的方法是在中再次查询clients表 Then the following is returned . 您可以使用表扩展方法comparetoset(…),而不是自己手动执行Assert。

相关问题