I'm asking about only sync
methods of the SqlCommand
class. There are three methods (as everyone know) - ExecuteReader()
, ExecuteScalar()
and ExecuteNonQuery()
.
我只询问SqlCommand类的同步方法。有三种方法(众所周知) - ExecuteReader(),ExecuteScalar()和ExecuteNonQuery()。
Which kind of this methods is more suitable for stored procedure likes this :
哪种方法更适合存储过程喜欢这样:
CREATE PROCEDURE [dbo].[pr_test]
@partherId UNIQUEIDENTIFIER,
@lowerBound SMALLINT = -1 out,
@upperBound SMALLINT = -1 out
AS
BEGIN
SET NOCOUNT ON;
SELECT
@lowerBound = ISNULL(MIN(SrartDayNumber), -1)
,@upperBound = ISNULL(MAX(EndDayNumber), -1)
FROM [CpsOther].[dbo].[FinDocument] f
WHERE f.partherId = @partherId
END
I need only out
params and nothing else. I don't know which method of the SqlCommand
is more suitable in this situation? Or it's doesn't matter. (The results are same)
我只需要params而不需要别的。我不知道SqlCommand的哪种方法更适合这种情况?或者没关系。 (结果相同)
int lowerBound = -1;
int upperBound = -1;
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "[dbo].[pr_test]";
SqlParameter lowerBoundParam = new SqlParameter
{
ParameterName = "@lowerBound",
Value = lowerBound,
Direction = ParameterDirection.Output
};
SqlParameter upperBoundParam = new SqlParameter
{
ParameterName = "@upperBound",
Value = upperBound,
Direction = ParameterDirection.Output
};
command.Parameters.AddWithValue("@partnerId", Guid.Empty);
command.Parameters.Add(lowerBoundParam);
command.Parameters.Add(upperBoundParam);
connection.Open();
object result = command.ExecuteScalar();
//or object result = command.ExecuteNonQuery();
lowerBound = lowerBoundParam.Value as int? ?? -1;
lowerBound = lowerBoundParam.Value as int? ?? -1;
}
}
1 个解决方案
#1
4
ExecuteNonQuery
is the better solution for this. The other two are for commands that return a rowset.
ExecuteNonQuery是更好的解决方案。另外两个用于返回行集的命令。
To elaborate:
详细说明:
-
ExecuteReader
is for situations where you want to iterate over a set of rows being returned by the command.ExecuteReader适用于您希望迭代命令返回的一组行的情况。
-
ExecuteScalar
is for situations where you want to receive the first column of the first row being returned. It will automatically discard all other row data.ExecuteScalar适用于您希望接收返回的第一行的第一列的情况。它会自动丢弃所有其他行数据。
-
ExecuteNonQuery
is for commands that do not return rowsets directly.ExecuteNonQuery适用于不直接返回行集的命令。
They all have the same abilities as regards parameters with directions of Output, InputOutput or ReturnValue. The only difference is how they deal with rowsets.
它们在具有Output,InputOutput或ReturnValue方向的参数方面具有相同的能力。唯一的区别是它们如何处理行集。
#1
4
ExecuteNonQuery
is the better solution for this. The other two are for commands that return a rowset.
ExecuteNonQuery是更好的解决方案。另外两个用于返回行集的命令。
To elaborate:
详细说明:
-
ExecuteReader
is for situations where you want to iterate over a set of rows being returned by the command.ExecuteReader适用于您希望迭代命令返回的一组行的情况。
-
ExecuteScalar
is for situations where you want to receive the first column of the first row being returned. It will automatically discard all other row data.ExecuteScalar适用于您希望接收返回的第一行的第一列的情况。它会自动丢弃所有其他行数据。
-
ExecuteNonQuery
is for commands that do not return rowsets directly.ExecuteNonQuery适用于不直接返回行集的命令。
They all have the same abilities as regards parameters with directions of Output, InputOutput or ReturnValue. The only difference is how they deal with rowsets.
它们在具有Output,InputOutput或ReturnValue方向的参数方面具有相同的能力。唯一的区别是它们如何处理行集。