- Recently we changed our DAC layer from using inline SQL to stored procedures in the database. On some of these SQL, we did record deletion (usually only 1 record), and we just execute it via IDBCommand.ExecuteNonQuery() and then check the return value to see how many records were affected (which should be 1) for verification that the query actually does something.
With the change to stored procedure, we just return 1 in the stored procedure if the delete is successful. However, the calling code then started to show these deletions as errors. Apparently ExecuteNonQuery only returns the number of affected rows on SELECT, INSERT and DELETE statements; for everything else it returns -1. So I tried to figure out how to get a return value from a stored procedure.
Let's assume a simplistic stored procedure as follows:
ALTER PROC ReturnOnlyASBEGINRETURN 5ENDprivate int ExecuteStoredProcedure(string storedProcedureName){SqlConnection connection = new SqlConnection(connectionString);// Command - specify as StoredProcedureSqlCommand command = new SqlCommand(storedProcedureName, connection);command.CommandType = CommandType.StoredProcedure;// Return value as parameterSqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);returnValue.Direction = ParameterDirection.ReturnValue;command.Parameters.Add(returnValue);// Execute the stored procedureconnection.Open();command.ExecuteNonQuery();connection.Close();return Convert.ToInt32(returnValue.Value);}
If you would like an alternative to randomly approaching girls and trying to figure out the right thing to say...
ReplyDeleteIf you'd rather have women pick YOU, instead of spending your nights prowling around in noisy pubs and nightclubs...
Then I encourage you to view this short video to find out a amazing secret that has the power to get you your personal harem of attractive women:
FACEBOOK SEDUCTION SYSTEM...