使用三元运算符填充 SqlCommand 参数
Using a ternary operator to fill a SqlCommand parameter
我需要在 SqlCommand
中填充一个参数,但在某些情况下我想用 DBNull.Value
填充它,否则用一个值填充它。
我需要的是当变量truckID == -1
然后将DBNull.Value
放入参数中,否则将truckID
的值放入其中。
这是我试过的:
using (SqlCommand command = new SqlCommand(sql.ToString()))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? DBNull.Value : truckID);
但是编译器告诉我:
Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'int'
如果我这样写:
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? 0 : truckID);
那么编译器很高兴。因此,对于三元运算符,两个可能的值似乎必须属于同一类型。
执行此操作的最佳方法是什么?
编辑:
我的工作代码是这样的:
command.Parameters.Add
(new SqlParameter("@TruckID", SqlDbType.Int).Value = (import.TruckID == -1) ? (object)DBNull.Value : import.TruckID);
编辑:
其实上面的代码终究还是不行。
在运行时我得到了这个:
SqlParameterCollection only accepts non-null SqlParameter type
objects, not DBNull objects
所以我修改了代码,最终对我有用。
command.Parameters.Add
(new SqlParameter("@PlaceID", SqlDbType.Int) { Value = (import.TruckID == -1) ? (object)DBNull.Value : import.TruckID });
您可以明确地将 DBNull.Value
转换为对象:
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? (object)DBNull.Value : truckID);
你可以这样试试
SqlParameter param = new SqlParameter("@truckID", System.Data.SqlDbType.Int);
param.Value = (truckID == -1) ? DBNull.Value : truckID;
command.Parameters.Add(param);
好吧,一个班轮版本是
SqlParameter param = new SqlParameter("@truckID", System.Data.SqlDbType.Int)
{ Value = (truckID == -1) ? (object)DBNull.Value : truckID };
command.Parameters.Add(param);
另一个版本:
SqlParameter param = (truckID == -1) ?
new SqlParameter("@truckID", System.Data.SqlDbType.Int) { Value = DBNull.Value }
: new SqlParameter("@truckID", System.Data.SqlDbType.Int) { Value = truckID };
command.Parameters.Add(param);
我需要在 SqlCommand
中填充一个参数,但在某些情况下我想用 DBNull.Value
填充它,否则用一个值填充它。
我需要的是当变量truckID == -1
然后将DBNull.Value
放入参数中,否则将truckID
的值放入其中。
这是我试过的:
using (SqlCommand command = new SqlCommand(sql.ToString()))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? DBNull.Value : truckID);
但是编译器告诉我:
Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'int'
如果我这样写:
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? 0 : truckID);
那么编译器很高兴。因此,对于三元运算符,两个可能的值似乎必须属于同一类型。
执行此操作的最佳方法是什么?
编辑:
我的工作代码是这样的:
command.Parameters.Add
(new SqlParameter("@TruckID", SqlDbType.Int).Value = (import.TruckID == -1) ? (object)DBNull.Value : import.TruckID);
编辑:
其实上面的代码终究还是不行。
在运行时我得到了这个:
SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects
所以我修改了代码,最终对我有用。
command.Parameters.Add
(new SqlParameter("@PlaceID", SqlDbType.Int) { Value = (import.TruckID == -1) ? (object)DBNull.Value : import.TruckID });
您可以明确地将 DBNull.Value
转换为对象:
command.Parameters.AddWithValue("@TruckID", truckID == -1 ? (object)DBNull.Value : truckID);
你可以这样试试
SqlParameter param = new SqlParameter("@truckID", System.Data.SqlDbType.Int);
param.Value = (truckID == -1) ? DBNull.Value : truckID;
command.Parameters.Add(param);
好吧,一个班轮版本是
SqlParameter param = new SqlParameter("@truckID", System.Data.SqlDbType.Int)
{ Value = (truckID == -1) ? (object)DBNull.Value : truckID };
command.Parameters.Add(param);
另一个版本:
SqlParameter param = (truckID == -1) ?
new SqlParameter("@truckID", System.Data.SqlDbType.Int) { Value = DBNull.Value }
: new SqlParameter("@truckID", System.Data.SqlDbType.Int) { Value = truckID };
command.Parameters.Add(param);