Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果列没有值,如何返回null_C#_Sql_Asp.net_Web Services - Fatal编程技术网

C# 如果列没有值,如何返回null

C# 如果列没有值,如何返回null,c#,sql,asp.net,web-services,C#,Sql,Asp.net,Web Services,根据本例,如果列没有值,并且对于某些特定字段返回空白(仅为“”),而没有任何字符,如何返回null [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public DataTable GetEmployeeInformation(string USERID)

根据本例,如果列没有值,并且对于某些特定字段返回空白(仅为“”),而没有任何字符,如何返回null

       [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public DataTable GetEmployeeInformation(string USERID)
        {
            string constr = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM VW_NTB_EIM_O365 WHERE USERID=@USERID"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Parameters.AddWithValue("@USERID", USERID);
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            dt.TableName = "NTB_EIM_O365";
                            sda.Fill(dt);
                            if (dt == null)
                            {
                                return null;
                            }

                            return dt;
                        }
                    }
                }
            }
        }
    }
}

问题是,您正在处理返回的数据表

using (DataTable dt = new DataTable())   // <---- using calls the Dispose()
{
    dt.TableName = "NTB_EIM_O365";
    sda.Fill(dt);
    if (dt == null)
    {
        return null;
    }

    return dt;
}

使用(DataTable dt=new DataTable())//空值将直接来自DB(如果它们在DB中针对特定行/列),无论如何,如果您想检查数据表中某个单元格的空值/任何其他条件,下面是代码

if(dt != null && dt.Rows.count > 0){
      foreach(DataRow dr in dt.Rows)
                {
                    if (string.IsNullOrWhiteSpace(dr.Field<string>("col")))
                    {
                        //do something
                        dr["somecol"] = null;
                    }
                    if (dr.Field<string>("somecol") == "someValue")
                    {
                        dr["somecol"] = string.Empty;
                        //do something
                    }
                }
}
else 
{
    return null;
}
if(dt!=null&&dt.Rows.count>0){
foreach(数据行dr在dt.行中)
{
if(string.IsNullOrWhiteSpace(dr.Field(“col”))
{
//做点什么
dr[“somecol”]=null;
}
if(dr.Field(“somecol”)=“someValue”)
{
dr[“somecol”]=string.Empty;
//做点什么
}
}
}
其他的
{
返回null;
}

可能是这样的:

public bool EmptyDataTable(DataTable dt)
{
    if (dt == null || dt.Rows.Count < 1)
    return true;

    for (int i = 0; i < dt.Rows.Count; i++)
    {
         if (!string.IsNullOrWhiteSpace(dt.Rows[i].Field<string>("columnName")))
         {
             return false;
         }
     }

     return true;
}
你可以试试这个

  if (dt != null && dt.Rows.Count>0)
         return dt;
      else
         return null;

if (EmptyDataTable(dt))
{
     return null;
}
  if (dt != null && dt.Rows.Count>0)
         return dt;
      else
         return null;
  if(dt !=null)
    {
      if(dt.Rows.Count>0)
      {
          return dt;
      }
   }
     return null;