How I do know if SQL Server Stored Procedure that performs an Update worked?

Francisc0

Say I have this stored procedure that I have no control over (and no access to the third party db).

How do I know if it worked?

BEGIN
        Update USR
        Set usr_psswrd = @NewPassword
        where
            usr_usrnme = @UserName and usr_psswrd = @OldPassword
END

I know how to get rows when there's a select statement in a stored procedure and read those rows but I have no idea how to check if this stored procedure worked or not.

This is what I'm doing so far that doesn't work. The stored procedure works because the password does change I just don't know what to do after the fact.

using (SqlConnection connection = new SqlConnection(connectionString))
{
            // Create the command and set its properties.
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "USP_ChangePassword";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = email;
            command.Parameters.Add("@OldPassword", SqlDbType.VarChar).Value = oldPW;
            command.Parameters.Add("@NewPassword", SqlDbType.VarChar).Value = newPW;

            try
            {
                // Open the connection and execute the reader.
                connection.Open();
                command.ExecuteNonQuery();

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    success = true;
                }

                reader.Close();

            }
            catch (SqlException ex)
            {
                System.Diagnostics.Debug.Write("SqlException Error " + ex.Number + ": " + ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                System.Diagnostics.Debug.Write("Invalid Op Error: " + ex.Message);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write("Error: " + ex.Message);
            }
            finally
            {
                connection.Close();
            }
}
Steve

IN the documentation about ExecuteNonQuery you could find

> Return Value 
> Type: System.Int32 
> The number of rows affected.

So you could change your code to

try
{
    // Open the connection and execute the reader.
    connection.Open();
    int rowsUpdated = command.ExecuteNonQuery();
    if(rowsUpdated > 0)
    {
        success = true;
    }
}

This is the normal behavior of ExecuteNonQuery, but check if your stored procedure contains the statements

SET NOCOUNT ON

if you have this line, then ExecuteNonQuery cannot return the number of rows affected and you get always a -1 as return value. If you cannot change that stored procedure, then you are in trouble.

The only workaround that comes to mind is to get back the user data with a SELECT query and check against the inserted data (a very uncomfortable situation)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I back up a stored procedure in SQL Server?

분류에서Dev

how to secure a valuable stored procedure in sql server

분류에서Dev

How to pass schema as parameter to a stored procedure in sql server?

분류에서Dev

How to parse a VARCHAR passed to a stored procedure in SQL Server?

분류에서Dev

SQL Stored Procedure Get Distinct and Update

분류에서Dev

Converting Stored Procedure into a query (SQL Server Compact)?

분류에서Dev

Varchar value not passing into SQL Server stored procedure

분류에서Dev

SQL server create stored procedure syntax error

분류에서Dev

SQL stored procedure: how to concatenate parameter value?

분류에서Dev

how to split a xml format data into row column format in sql server 2008 using stored procedure

분류에서Dev

SQL server stored procedure issue in via with c#

분류에서Dev

SQL Server : stored procedure many-to-many table query

분류에서Dev

Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

분류에서Dev

SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

분류에서Dev

SQL Server Stored procedure aborting when called from php

분류에서Dev

Nested stored procedure in SQL

분류에서Dev

SQL Server 2008 R2 - Running a Stored Procedure against Linked Servers

분류에서Dev

SQL Server CLR Library Stored Procedure static object instantiated more than once

분류에서Dev

Return result set from SQL Server stored procedure to vb.net

분류에서Dev

Emailing from SQL Server stored procedure with set-based logic, avoiding cursors

분류에서Dev

How do i know if the rotation lock is on or off?

분류에서Dev

How do I get the char index of an exact word in SQL Server

분류에서Dev

How do I join multiple tables in SQL Server?

분류에서Dev

Call stored procedure from PL/SQL Job

분류에서Dev

PL/SQL Stored Procedure - IF THEN ELSE condition

분류에서Dev

SQL Stored procedure does not enter IF and CASE condition

분류에서Dev

How can I call an Oracle stored procedure with object parameter for input in c#?

분류에서Dev

How do I know I'm using kubuntu?

분류에서Dev

SQL SERVER Select MAX Procedure

Related 관련 기사

  1. 1

    How can I back up a stored procedure in SQL Server?

  2. 2

    how to secure a valuable stored procedure in sql server

  3. 3

    How to pass schema as parameter to a stored procedure in sql server?

  4. 4

    How to parse a VARCHAR passed to a stored procedure in SQL Server?

  5. 5

    SQL Stored Procedure Get Distinct and Update

  6. 6

    Converting Stored Procedure into a query (SQL Server Compact)?

  7. 7

    Varchar value not passing into SQL Server stored procedure

  8. 8

    SQL server create stored procedure syntax error

  9. 9

    SQL stored procedure: how to concatenate parameter value?

  10. 10

    how to split a xml format data into row column format in sql server 2008 using stored procedure

  11. 11

    SQL server stored procedure issue in via with c#

  12. 12

    SQL Server : stored procedure many-to-many table query

  13. 13

    Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

  14. 14

    SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

  15. 15

    SQL Server Stored procedure aborting when called from php

  16. 16

    Nested stored procedure in SQL

  17. 17

    SQL Server 2008 R2 - Running a Stored Procedure against Linked Servers

  18. 18

    SQL Server CLR Library Stored Procedure static object instantiated more than once

  19. 19

    Return result set from SQL Server stored procedure to vb.net

  20. 20

    Emailing from SQL Server stored procedure with set-based logic, avoiding cursors

  21. 21

    How do i know if the rotation lock is on or off?

  22. 22

    How do I get the char index of an exact word in SQL Server

  23. 23

    How do I join multiple tables in SQL Server?

  24. 24

    Call stored procedure from PL/SQL Job

  25. 25

    PL/SQL Stored Procedure - IF THEN ELSE condition

  26. 26

    SQL Stored procedure does not enter IF and CASE condition

  27. 27

    How can I call an Oracle stored procedure with object parameter for input in c#?

  28. 28

    How do I know I'm using kubuntu?

  29. 29

    SQL SERVER Select MAX Procedure

뜨겁다태그

보관