Why do I need to specify a return value for an SQL query?

I have a method that either adds or updates a record in the database (SQL Server), and returns the RecordID as the output parameter (Int32), as well as the success / failure result as (Int32). Return value

Given that I indicate the type of these parameters, why should I drop them when they return?

I expected to use the following:

enquiryID = cmd.Parameters["@EnquiryID"].Value;

... but I and I have to jump through a couple of extra hoops:

enquiryID = Int32.Parse(cmd.Parameters["@EnquiryID"].Value.ToString());

This is not the end of the world, but it just looks like a long-term solution. Why does Parameters.Value return an SqlParameters.Value rather than Int32?

UPDATE:

OK, I am convinced - FTW direct casting: (int)cmd.Parameters["@EnquiryID"].Value

+3
2

SQL Server , # . , SqlParamater , SQL Server . SQL Server .

, Int32.Parse(cmd.Parameters["@EnquiryID"].Value.ToString()), , int, (int)cmd.Parameters["@EnquiryID"].Value. null,

object value = cmd.Parameters["@EnquiryID"].Value;
if(!value.Equals(DBNull.Value)
    enquiryID = (int)value;
+5

Value SqlParameter object, , .

, , int32. , Value valuey, - , :

enquiryID = (int)cmd.Parameters["@EnquiryID"].Value;
+2

Source: https://habr.com/ru/post/1752440/


All Articles