2011-12-06 65 views
1

我收到一个参数并从存储过程获取特定产品。这个存储过程返回很多列(我认为大约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; 
    } 
+1

我强烈建议使用LINQ-to-SQL将您的数据库映射到类中,这使得这些事情变得非常简单和容易。 –

回答

7
int? x = dt.Field<int?>("Field"); 

或者

int y = dt.Field<int?>("Field") ?? 0; 

C# DBNull and nullable Types - cleanest form of conversion

顶部可能是你最好的办法。您目前有一些不可填充的类型正在填充。所以如果你想能够将它们表示为null,请使用顶级方法。否则,您可以使用其他选项给出默认值。

+0

我已将可空类型与下面的解决方案结合使用。它效果很好。我为它运行了许多单元测试。我也喜欢你的建议。 – gsirianni

0

对您可能怀疑为空值的字段执行DBNULL检查。在这种情况下,所有这些。

if (! DBNull.Value.Equals(row[fieldName])) 
     return (string) row[fieldName] + " "; 
    else 
     return String.Empty; 

你可以做的是创造一个具有用于检查空

因此,这将是这个样子整型,字符串和日期时间参数几个重载函数:

public string CheckNull(string value) 
{ 
    if (! DBNull.Value.Equals(value)) 
      return value; 
     else 
      return String.Empty; 
} 

这样你就只需要做点像

object.stringProperty = (string)CheckNull(row["columnName"]); 
1

LINQ将会是你的ST朋友在这几种情况,如SSG的意见建议,但是如果你想使一个非常大的除了空检查的,你可以实现类似如下:

int productID = (myReader["ProductID"] != null) ? Convert.ToInt16(myReader["ProductID"]) : 0; 

,或者你可以简单地使用如果声明。

0

当使用数据阅读器和DTO之间的直接映射时,我使用数据阅读器的IsDbNull()方法。以下是使用不同数据类型的几个示例:

myObj.MyStringProperty = !myReader.IsDBNull("myStringColumn") ? myReader["myStringColumn"].ToString() : null; 
myObj.MyNullableDecimalProperty = !myReader.IsDBNull("mydecimalColumn") ? decimal.Parse(myReader["mydecimalColumn"].ToString()) : new decimal?(); 
相关问题