字符串数组与c#中的数据集值进行比较

时间:2014-12-24 12:42:41

标签: c# .net dataset

我有以下代码:

DataSet ds = new DataSet();
ds = cls.ReturnDataSet("RetriveData",
             new SqlParameter("@Field", "mark1"),
             new SqlParameter("@TblNm", "stud"),
             new SqlParameter("@WhereClause", "where id=124"));

有了这个我得到低于值:

Id   mark1
124     21 
124     31
124     41 
124     23
124     35
124     56
124     67
124     54
124     45
124     63

现在从下面我得到学生标记:

DataSet dsmark = new DataSet();
dsmark = cls.ReturnDataSet("RetriveData",
                 new SqlParameter("@Field", "marks"),
                 new SqlParameter("@TblNm", "student"),
                 new SqlParameter("@WhereClause", "where id=124"));

从上面的查询我得到以下输出:

Id    marks
124    63

以下代码进行比较:

 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["mark1"].ToString() != dsmark .Tables[0].Rows[0]["marks"].ToString())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('err')", true);
            }
           else
            {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('No err')", true);
            }
        }

但是,当我这样做时,它会比较值,但每当条件调用并且不满足时,它会给我消息&#34;错误&#34;。

但那&#34; 63&#34;数据库中的值..

so i want like it will check with the all values and then if that values is not match then and then only give me message "err".

2 个答案:

答案 0 :(得分:1)

示例代码,

foreach(DataRow row in ds.Tables[0].Rows)
{
   //your code to compare.
}

答案 1 :(得分:1)

您的结果存储在DataSet内的DataTable内。所以你可以将第一组迭代为

foreach(DataRow dro in ds.Tables[0].Rows)
{
   // your comparison logic here
}

并将该表的mark1列的值与dsMark.Tables[0].Rows[0]["marks"](或您需要的任何其他比较)进行比较

<强>更新

根据更新的问题 - 您的比较逻辑不正确。为了实现你的目标,应该是这样的:

bool matchFound = false;

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    if (ds.Tables[0].Rows[i]["mark1"].ToString() == dsmark .Tables[0].Rows[0]["marks"].ToString())
        matchFound = true;
}

if (matchFound)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('No err')", true);
else
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('err')", true);
相关问题