String Array compare with dataset values in c#

邮差的信 提交于 2020-01-15 04:02:07

问题


I have following code:

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

with this I am getting below values:

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

Now from below I am getting students mark:

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

From above query I am getting the below output:

Id    marks
124    63

Below code for comparison:

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

But when i do this it will compare the values but when every time if condition call and it is not satisfy then it will give me message "err".

But that "63" values in the database..

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".

回答1:


Your results are stored in DataTable inside of DataSet. So you can iterate first set as

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

and compare values of column mark1 of that table with dsMark.Tables[0].Rows[0]["marks"] (or any other comparison you need)

UPDATE

Based on updated question - your comparison logic is incorrect. To achieve your goals it should be something like:

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



回答2:


Sample code,

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


来源:https://stackoverflow.com/questions/27637134/string-array-compare-with-dataset-values-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!