如何获取数据表的列并检查它是否为空 [英] How to get datatable's column and check it is null

查看:78
本文介绍了如何获取数据表的列并检查它是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要你的帮助。我写了SP和获取数据的相关方法

  public  DataTable GetLatestQuantityOfProduct(Business.Stock objStock)
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt1 = new DataTable();
尝试
{
使用(SqlConnection conn = Connection.OpenConnection ())
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = GetLatestQuantityOfProduct;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(dt1);
}

}
catch (例外情况)
{
log.Error( System.Reflection.MethodBase.GetCurrentMethod()。ToString()+ ex.StackTrace);
}
return dt1;

}



所以我需要:

1)。首先需要从数据表中获取ProductID列或字段

2)。检查ProductID是否为null



我在这里写了一些代码

 DataTable dtQuantityData =  new  DataTable();  //  这是数据表 
dtQuantityData = objStockData。 GetLatestQuantityOfProduct(objStockBusiness); // 此处称为方法



所以我需要你的帮助,或者有其他办法,所以你可以建议我

提前提前

解决方案

你可以尝试一下如下所示



 foreach(dtQuantityData.Rows中的DataRow行)
{
object val = row [ColumnName ]。
if(val == DBNull.Value)
//您的代码
其他
//您的代码
}



希望这有助于


关于从数据行列中获取值,您可以使用 Item 属性。请参阅 DataRow.Item属性(字符串) [ ^ ]



什么来的对于第二个问题,JoCodes的答案是正确的,你可以使用 DBNull.Value ,但让我感到困惑的是你的存储过程的名称是 GetLatestQuantityOfProduct 。现在,如果存储过程将在 ProductID 中返回 NULL 的行,这是否意味着您返回金额但是不知道产品:confused:



我想要的是你应该修改存储过程中的SQL来消除这种行或者别的什么。



当然我可能会走错路,因为你没有描述逻辑,而我只是根据名字猜测:)


你可以尝试以下条件来检查空值



 row.IsNull(myColumn)


Hi guys
I need your help.I wrote SP and related method for getting data

public DataTable GetLatestQuantityOfProduct(Business.Stock objStock)
      {
          SqlDataAdapter da = new SqlDataAdapter();
          DataTable dt1 = new DataTable();
          try
          {
              using (SqlConnection conn = Connection.OpenConnection())
              {
                  SqlCommand cmd = new SqlCommand();
                  cmd.CommandText = "GetLatestQuantityOfProduct";
                  cmd.CommandType = System.Data.CommandType.StoredProcedure;
                  cmd.Connection = conn;
                  da.SelectCommand = cmd;
                  da.Fill(dt1);
              }

          }
          catch (Exception ex)
          {
              log.Error(System.Reflection.MethodBase.GetCurrentMethod().ToString() + ex.StackTrace);
          }
          return dt1;

      }


So i need to:
1). Firstly need to get ProductID column or field from datatable
2). Check the ProductID is null or not

I wrote here some code here

DataTable dtQuantityData = new DataTable();//this is datatable 
dtQuantityData = objStockData.GetLatestQuantityOfProduct(objStockBusiness);//called method here


So i need your help or there is other way so you can suggest me
thanxx in advance

解决方案

You can try something like below

foreach(DataRow row in dtQuantityData.Rows)
{
    object val = row["ColumnName"];
    if (val == DBNull.Value)
        // your code
    else
        //your code
}


Hope this helps


About getting a value from a datarow column, you use the Item property. See DataRow.Item Property (String)[^]

What comes to the second question, the answer from JoCodes is correct, you can use the DBNull.Value, but what puzzles me is that the name of your stored procedure is GetLatestQuantityOfProduct. Now if the stored procedure would return rows with NULL in ProductID, wouldn't that mean that you return amounts but you don't know the product :confused:

What I'm after is that should you instead modify the SQL inside the stored procedure to possibly eliminate this kinds of rows or something else.

Of course I may be on a wrong track since you haven't described the logic and I'm just guessing based on the names :)


you can try below condition to check for null value

row.IsNull("myColumn")


这篇关于如何获取数据表的列并检查它是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆