如何在c#中清空数据集 [英] How to dataset empty in c#

查看:610
本文介绍了如何在c#中清空数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这里我使用业务逻辑方法返回数据集。对我来说,数据集返回null。如何检查。

例如:

  for  int  i =  0 ; i <  dsAccSId1.Tables [ 0 ]。Rows.Count; i ++)
{
string company = dsAccSId1.Tables [ 0 ]。行[i] [ ACCID]的ToString();
string getbill = 从tblbillmaster中选择*其中company =' + company + ';
DataSet getcomp = dbObj.InlineExecuteDataSet(getbill);
if (getcomp.Tables [ 0 ]。Rows.Count > 0
{
ds.Merge(getcomp);
}
}
返回 ds;



以上示例此处为i合并数据集。如果计数不存在那么ds应该是空的,即使列不绑定。如何解决这个问题。



我尝试了什么:



这里我检查null但没有用它会处理下一步然后我得到错误表找不到0

解决方案

尝试:

  if (getcomp!=  null && getcomp.Tables.Count >   0 
{
...
}

但正如理查德所说:不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

Google为Bobby Tables并且不要只是假设它是个笑话!


之前我遇到了同样的问题所以我使用了什么是..

 if(dsAccSId1!= null&& dsAccSId1.Tables.Count> 0&& dsAccSId1.Tables [0] .Rows.Count> ; 0)
{

}



可能会对你有帮助......


< blockquote>使用以下语句

  if (ds.Tables [ 0 ]。Rows.Count ==  0 
{
/ /
}


hi to all,
Here i using business logic that method return dataset. for me that dataset return null. how to check.
For eg:

for (int i = 0; i < dsAccSId1.Tables[0].Rows.Count; i++)
{
  string company = dsAccSId1.Tables[0].Rows[i]["Accid"].ToString();
  string getbill = "Select * from tblbillmaster where company ='" + company + "'";
  DataSet getcomp = dbObj.InlineExecuteDataSet(getbill);
  if (getcomp.Tables[0].Rows.Count > 0)
  {
    ds.Merge(getcomp);
  }
}
return ds;


above example here i merging dataset .if the count not exists then ds should be empty even column not bind.how to resolve this problem.

What I have tried:

here i check null but it no use it will process next step and then i got error table cannot find 0

解决方案

Try:

if (getcomp != null && getcomp.Tables.Count > 0)
   {
   ...
   }

But as Richard says: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Google for "Bobby Tables" and don't just assume it's a joke!


I had same problem before so what i had used is..

if (dsAccSId1 != null && dsAccSId1.Tables.Count > 0 && dsAccSId1.Tables[0].Rows.Count > 0)
{
	
}


May be this will help you...


Use the following statement

if (ds.Tables[0].Rows.Count == 0)
{
    //
}


这篇关于如何在c#中清空数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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