在C#中的数据集值字符串数组比较 [英] String Array compare with dataset values in c#
问题描述
我有以下code:
的DataSet ds为新的DataSet();
DS = cls.ReturnDataSet(RetriveData
新的SqlParameter(@域,标记1),
新的SqlParameter(@ TblNm,双头),
新的SqlParameter(@ WhereClause,其中id = 124));
这个我得到以下值:
标识MARK1
124 21
124 31
124 41
124 23
124 35
124 56
124 67
124 54
124 45
124 63
现在从下面我让学生标记:
的DataSet DSMARK =新的DataSet();
DSMARK = cls.ReturnDataSet(RetriveData
新的SqlParameter(@域,商标),
新的SqlParameter(@ TblNm,学生),
新的SqlParameter(@ WhereClause,其中id = 124));
从上面的查询我得到了下面的输出:
标识标志
124 63
下面code进行比较:
的for(int i = 0; I< ds.Tables [0] .Rows.Count;我++)
{
如果(ds.Tables [0] .Rows [I] [MARK1]。的ToString()!= DSMARK .Tables [0] .Rows [0] [标记]。的ToString())
{
Page.ClientScript.RegisterStartupScript(this.GetType(),消息,警报('犯错'),真正的);
}
其他
{
Page.ClientScript.RegisterStartupScript(this.GetType(),消息,警戒('没有犯错),真正的);
}
}
但是,当我做到这一点会比较值,但是当每一个时间,如果条件呼叫,它无法满足那么就会给我留言犯错。
不过,在数据库中63的价值观..
所以我要像它会检查与所有的值,然后如果该值不匹配,那么,然后只给我留言犯错。
您的结果存储在的 >数据集
。所以,你可以遍历先设置为
的foreach(DataRow的DRO在ds.Tables [0] .Rows)
{
//你这里比较的逻辑
}
和比较列的值标记1
与 dsMark.Tables [0] .Rows [0] [标记] 该表code>(或者你需要的任何其他比较)
更新
根据更新的问题 - 你的比较逻辑是不正确。为了实现自己的目标就应该是这样的:
布尔matchFound = FALSE;
的for(int i = 0; I< ds.Tables [0] .Rows.Count;我++)
{
如果(ds.Tables [0] .Rows [I] [MARK1]。的ToString()== DSMARK .Tables [0] .Rows [0] [标记]。的ToString())
matchFound = TRUE;
}
如果(matchFound)
Page.ClientScript.RegisterStartupScript(this.GetType(),消息,警戒('没有犯错),真正的);
其他
Page.ClientScript.RegisterStartupScript(this.GetType(),消息,警报('犯错'),真正的);
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".
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);
这篇关于在C#中的数据集值字符串数组比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!