是否有一种简单的方法来检查多个列,以查看值是否为空
本文关键字:是否 一种 方法 简单 检查 | 更新日期: 2023-09-27 18:17:12
我正在从存储过程中获取参数和特定产品。这个存储过程返回许多列(我认为大约15-20列)。不幸的是,其中一些值可能为空,当我试图将它们放入变量中时,这当然会产生错误。是否有一种简单的方法来检查和查看这些值是否为NULL
,以便它不会抛出异常,或者我是否需要在每次赋值之前放置if
语句?如果有帮助,下面是我的代码:
public List<Product> ReadSpecificProductDetails(string productAndURLID)
{
ConfigureDataAccess();
myCommand = new SqlCommand("[dbo].[GetSpecificProductDetails]", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
//myCommand.Parameters.Add(productAndURLID);
//myCommand.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.SmallInt));
myCommand.Parameters.AddWithValue("@ProductID", Convert.ToInt16(productAndURLID));
try
{
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
}
catch (SqlException exception)
{
exception.Data.Add("cstring", myConfiguration);
throw;
}
catch (InvalidOperationException ex)
{
ex.Data.Add("cStatus", myConnection.State);
throw;
}
//Create a 'Product' list to store the records in
while (myReader.Read())
{
int productID = Convert.ToInt16(myReader["ProductID"]);
string productName = (string)myReader["Name"];
string productNumber = (string)myReader["ProductNumber"];
//int quantitySum = Convert.ToInt16(myReader["QuantitySum"]);
string productColor = (string)myReader["Color"];
int productSafetyStockLevel = Convert.ToInt16(myReader["SafetyStockLevel"]);
int productReorderPoint = Convert.ToInt16(myReader["ReorderPoint"]);
decimal productStandardCost = Convert.ToDecimal(myReader["StandardCost"]);
decimal productListPrice = Convert.ToDecimal(myReader["ListPrice"]);
string productSize = (string)myReader["Size"];
decimal productWeight = Convert.ToDecimal(myReader["Weight"]);
int productDaysToManufacture = Convert.ToInt16(myReader["DaysToManufacture"]);
string productCategoryName = (string)myReader["PC.Name"];
string productSubCategoryName = (string)myReader["PS.Name"];
string productModelName = (string)myReader["PM.Name"];
DateTime productSellStartDate = Convert.ToDateTime(myReader["Sell_Start_Date"]);
DateTime productSellEndDate = Convert.ToDateTime(myReader["Sell_End_Date"]);
DateTime productDiscontinuedDate = Convert.ToDateTime(myReader["Discontinued_Date"]);
Product p = new Product(productID, productName, productNumber, productColor, productSafetyStockLevel, productReorderPoint, productStandardCost,
productListPrice, productSize, productWeight, productDaysToManufacture, productCategoryName, productSubCategoryName, productModelName, productSellStartDate,
productSellEndDate, productDiscontinuedDate);
myProducts.Add(p);
}
}
finally
{
myReader.Close();
myConnection.Close();
}
return myProducts;
}
int? x = dt.Field<int?>( "Field" );
或
int y = dt.Field<int?>( "Field" ) ?? 0;
c# DBNull和可空类型——最干净的转换形式
顶部可能是你最好的方法。您目前有一些要填充的非空类型。如果你想把它们表示为空,可以用上面的方法。否则,您可以使用其他选项来提供默认值。
LINQ将是你在这些类型的情况下最好的朋友,正如ssg在评论中建议的那样,但是如果你想做一个非常大的null检查,你可以实现如下的东西:
int productID = (myReader["ProductID"] != null) ? Convert.ToInt16(myReader["ProductID"]) : 0;
或者直接使用if语句
对可能怀疑为空值的字段执行DBNULL检查。
if (! DBNull.Value.Equals(row[fieldName]))
return (string) row[fieldName] + " ";
else
return String.Empty;
你可以做的是创建一个函数,该函数对int, string和DateTime参数进行重载,检查是否为null
所以它看起来像这样:
public string CheckNull(string value)
{
if (! DBNull.Value.Equals(value))
return value;
else
return String.Empty;
}
这样你只需要做类似
这样的操作object.stringProperty = (string)CheckNull(row["columnName"]);
在使用数据读取器和DTO之间的直接映射时,我使用数据读取器的IsDbNull()方法。下面是几个使用不同数据类型的示例:
myObj.MyStringProperty = !myReader.IsDBNull("myStringColumn") ? myReader["myStringColumn"].ToString() : null;
myObj.MyNullableDecimalProperty = !myReader.IsDBNull("mydecimalColumn") ? decimal.Parse(myReader["mydecimalColumn"].ToString()) : new decimal?();