有一个简单的方法来检查多个列,看看是否值为null? [英] Is there an easy way to check multiple columns to see if the value is null?
问题描述
我得到一个参数,并从存储过程中得到一个特定的产品。此存储过程返回的列(我认为15-20)。不幸的是,一些值可能为空,当我试图把它们放在一个变量本课程产生错误。有一个简单的方法来检查,看看是否这些值 NULL
所以它不会抛出异常或做我需要把如果
每次作业之前声明?如果有帮助,这是我的code:
公开名单<产品与GT; ReadSpecificProductDetails(字符串productAndURLID)
{
ConfigureDataAccess();
myCommand =新的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)); 尝试
{
尝试
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
}
赶上(SqlException异常)
{
exception.Data.Add(CString的myConfiguration);
扔;
}
赶上(InvalidOperationException异常前)
{
ex.Data.Add(cStatus,myConnection.State);
扔;
} //创建一个产品列表中保存记录
而(myReader.Read())
{
INT =的productID Convert.ToInt16(myReader [产品ID]);
字符串产品名称=(字符串)myReader [名称];
字符串productNumber =(字符串)myReader [ProductNumber];
// INT quantitySum = Convert.ToInt16(myReader [QuantitySum]);
字符串productColor =(字符串)myReader [颜色;
INT productSafetyStockLevel = Convert.ToInt16(myReader [SafetyStockLevel]);
INT productReorderPoint = Convert.ToInt16(myReader [ReorderPoint]);
小数productStandardCost = Convert.ToDecimal(myReader [StandardCost]);
小数productListPrice = Convert.ToDecimal(myReader [ListPrice]);
字符串productSize =(字符串)myReader [大小];
小数productWeight = Convert.ToDecimal(myReader [重量]);
INT productDaysToManufacture = Convert.ToInt16(myReader [DaysToManufacture]);
字符串productCategoryName =(字符串)myReader [PC.Name];
字符串productSubCategoryName =(字符串)myReader [PS.Name];
字符串productModelName =(字符串)myReader [PM.Name];
日期时间productSellStartDate = Convert.ToDateTime(myReader [Sell_Start_Date]);
日期时间productSellEndDate = Convert.ToDateTime(myReader [Sell_End_Date]);
日期时间productDiscontinuedDate = Convert.ToDateTime(myReader [Discontinued_Date]);
产品p =新产品(的productID,产品名称,productNumber,productColor,productSafetyStockLevel,productReorderPoint,productStandardCost,
productListPrice,productSize,productWeight,productDaysToManufacture,productCategoryName,productSubCategoryName,productModelName,productSellStartDate,
productSellEndDate,productDiscontinuedDate);
myProducts.Add(P);
}
}
最后
{
myReader.Close();
myConnection.Close();
}
我的产品回报;
}
诠释? X = dt.Field<&诠释GT(田);
或者
INT Y = dt.Field<&诠释GT;(田)? 0;
<一个href=\"http://stackoverflow.com/questions/1706405/c-sharp-dbnull-and-nullable-types-cleanest-form-of-conversion\">C#为DBNull和空类型 - 换算的清洁形式
顶部可能是你最好的办法。您现在有你填充一些非可空类型。因此,如果你希望能够重新present他们为null,则使用顶部的方法。否则,你可以用其他的选项给予一个默认值。
I'm getting a parameter and getting a specific product from a stored procedure. This stored procedure is returning many columns (I think around 15-20). Unfortunately, some of these values may be null and this of course produces an error when I try to put them in a variable. Is there a simple way to check and see if these values are NULL
so that it doesn't throw an exception or do I need to put an if
statement before each assignment? If it helps, here is my code:
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" );
Or
int y = dt.Field<int?>( "Field" ) ?? 0;
C# DBNull and nullable Types - cleanest form of conversion
The top is probably your best approach. You currently have some non nullable types you are populating. So if you want to be able to represent them as null use the top approach. Otherwise you can use the other option to give a default value.
这篇关于有一个简单的方法来检查多个列,看看是否值为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!