C# 字符串数组与c中的数据集值比较#
我有以下代码:C# 字符串数组与c中的数据集值比较#,c#,.net,dataset,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")); 通过这一点,我得到以下值
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);
}
}
示例代码
foreach(DataRow row in ds.Tables[0].Rows)
{
//your code to compare.
}
您的结果存储在
数据集
的内部。所以,您可以将第一个集合迭代为
foreach(DataRow dro in ds.Tables[0].Rows)
{
// your comparison logic here
}
并将该表的列mark1
的值与dsMark.Tables[0]。行[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);
bool matchFound=false;
对于(int i=0;i
新的SqlParameter(“@WhereClause”,“where id=124和mark1=63”)
?ReturnDataSet到底做什么?好吧,为什么不迭代第一组值并与第二组值进行比较呢?returndata set是一个执行storedprocedure并返回值的函数。我想将63的值与10个不同的值进行比较,如果它不相等,则给出错误消息。您的第一个集合不是数组。我想它是DataTable
。是的,但逻辑会改变,一个值可能等于ds
中的一个值,但可能不等于另一个值。如果在dsmark
中有不同的值,则比较不等于
将得出所有值都不相等的结论。
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);