如何检查数据集中的值是否为空?

时间:2016-09-02 06:10:36

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

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

.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);
}

3 个答案:

答案 0 :(得分:1)

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

 <xcad:DockingManager  Theme="What should I write here?">

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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);
         }
     }
}