Looking at some source code that I inherited, there is a piece of code that calls the SQL stored procedure that does the update.
The stored procedure returns -1 if something goes wrong:
IF @@Error <> 0 BEGIN ROLLBACK TRAN SELECT -1 RETURN END COMMIT TRAN SELECT 0
C # code looks something like this:
System.Data.SqlClient.SqlDataReader myReader; try{ SqlDbConnection.Open(); SqlDbCommand.Connection = SqlDbConnection; SqlDbCommand.CommandType = System.Data.CommandType.StoredProcedure; SqlDbCommand.CommandText = "StoredProcedured_UpdateFoo"; SqlDbCommand.Parameters.Clear(); SqlDbCommand.Parameters.Add("@FooData", SqlDbType.DateTime); SqlDbCommand.Parameters["@FooData"].Value = System.DateTime.Now.ToString("yyyy-MM-dd"); myReader = SqlDbCommand.ExecuteReader(); if (myReader.Read()) { if (int.Parse(myReader.GetValue(0).ToString()) == -1) throw new ErrorDataTxRx("Error FOO "); } } finally { if (SqlDbConnection.State != ConnectionState.Closed){ SqlDbConnection.Close(); } if (myReader != null){ if (!myReader.IsClosed) myReader.Close(); } }
I also see part of the same code, checking the same, using System.Data.DataSet () with the Fill method.
Is there a more elegant way to check if the return value is -1?
Can I use ExecuteReader in this case?
source share