【问题标题】:How to check a value in dataset is empty or not?如何检查数据集中的值是否为空?
【发布时间】:2017-01-10 03:47:09
【问题描述】:

我的项目中有一个文件上传选项。它包括一个返回数据集的查询。它工作正常。但是现在我想检查返回的数据集是空的还是我作为参数传递给查询的相同值。这是我的后端代码。

.cs 代码

if ((FileUpload1.HasFile))//&& (ext == ".pdf")
{
    ds = db.checkExistingPDF(fileName);
    if (dbFileName != fileName)
    {
         this.FileUpload1.SaveAs(Path.Combine(svrPath, fileName + ".pdf"));
         ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " alert('Successfully uploaded');", true);                    
    }
    else
    {
         ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " confirm ('Appeal is availbale for the this competition') ; ", true);  
    }    
else
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " confirm ('Error') ; ", true);
}

这是我的查询

public DataSet checkExistingPDF(string fileName)
{
    string strQuery = @"IF EXISTS (SELECT * FROM APPEAL_MASTER WHERE Attachment_upload = '"+ fileName +"')";

    return SqlHelper.ExecuteDataset(strConnStringAppeal, CommandType.Text, strQuery);
}

【问题讨论】:

  • 不是回复here吗?
  • ds.Tables[0].Rows.Count
  • @jakub 最重要的导入条件来检查输入参数是否存在于数据库中。检查它是否为空是可选的
  • @vivek kv 我想检查数据集的返回值是否与参数中的值相同。它具有最高优先级
  • string strQuery = @"IF EXISTS (SELECT * FROM APPEAL_MASTER WHERE Attachment_upload = '"+ fileName +"')".. 查询不正确.. 不是返回错误吗?它应该是字符串 strQuery = @"SELECT * FROM APPEAL_MASTER WHERE Attachment_upload = '"+ fileName +"'"

标签: c# asp.net sql-server file-upload dataset


【解决方案1】:

数据集对象中获取您的结果,然后验证 NULL 和表行数

Dataset ds=checkExistingPDF("filename");
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
    //record exist with same filename
}
else
{
    //no any record exist with same filename
}

【讨论】:

    【解决方案2】:

    要检查数据集是否为空,您必须检查 null 和表数。

    Dataset ds=checkExistingPDF("filename");
    if(ds != null && ds.Tables.count > 0)
    {
     // your code
    }
    

    【讨论】:

      【解决方案3】:
      DataSet dsReturnedObj = SqlHelper.ExecuteDataset(strConnStringAppeal, CommandType.Text, strQuery);
      return dsReturnedObj == null ? null : dsRetunredObj
      

      在你的cs文件后面的代码中:

      Dataset dsReturnedObj = db.checkExistingPDF(fileName)
      if (dsReturnedObj != null)
      {
           if (dsReturnedObj.Tables.Count > 0)
           {
               if (dsReturnedObj.Tables[0].Rows.Count > 0)
               {
                   this.FileUpload1.SaveAs(Path.Combine(svrPath, fileName + ".pdf"));
                   ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " alert('Successfully uploaded');", true);
               }
           }
      }
      

      【讨论】:

      • @Balagurunathan 请加代码检查返回值和参数值是否一致
      • @user6592730 如果您的参数不匹配,上述代码将返回空数据集。
      • 是的,我知道。但我想将 reurn 值与文件名进行比较,以了解它是否已经存在
      • 查询string strQuery = @"IF EXISTS (SELECT * FROM APPEAL_MASTER WHERE Attachment_upload = '"+ fileName +"')";显然返回了包含表和记录的数据集;如果文件名包含在表中。那为什么还要检查呢??
      猜你喜欢
      • 2012-02-28
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2018-12-24
      • 2020-03-29
      • 2016-03-28
      • 1970-01-01
      相关资源
      最近更新 更多