比较c#中的两个数据集数据 [英] Compare two datasets data in c#

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

问题描述

大家好,



我有一个数据集如

sno name qty

1 a 10

和另一个数据集

Sno CurrentStock

1 50

我的实际要求是我要查看sno''第一个数据集中的1''在第二个数据集中可用如果可用我想将qty从第一个数据集更新到表



并且sno 1在第二个数据集中不可用我需要在表格中添加新记录



请帮帮我





提前谢谢

Hi All,

I have one DataSet like
sno name qty
1 a 10
And another DataSet
Sno CurrentStock
1 50
My actual requirement is I want to check the sno ''1'' in first dataset is available in second dataset if it is available I want to update qty from first dataset to table

And sno 1 is not available in second dataset I need to insert a new record in table

Please help me


Thanks in advance

推荐答案

你好,

我以为你想要这类代码......



Hello,
I thought that you want this type of code like here...

SqlDataAdapter adp = new SqlDataAdapter("select * from tbl1", cn);
       DataSet ds = new DataSet();
       adp.Fill(ds);

       SqlDataAdapter adp1 = new SqlDataAdapter("select * from tbl2", cn);
       DataSet ds1 = new DataSet();
       adp1.Fill(ds1);

       if (ds.Tables[0].Rows[0][0].ToString()==ds1.Tables[0].Rows[0][0].ToString())
       {
           SqlCommand cmd = new SqlCommand("insert into tbl1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", cn);
           cn.Open();
           cmd.ExecuteNonQuery();
           cn.Close();

           SqlCommand cmd1= new SqlCommand("insert into tbl2 values('" + TextBox1.Text + "','" + TextBox3.Text + "')", cn);
           cn.Open();
           cmd1.ExecuteNonQuery();
           cn.Close();
       }


你好,



只是想改善以上解决方案。



而不是使用此支票

Hello,

Just wanted to improve the above solution.

Instead of using this check
if (ds.Tables[0].Rows[0][0].ToString()==ds1.Tables[0].Rows[0][0].ToString())
       {}





使用





use

string compare = ds1.Tables[0].Rows[0][0].ToString();
if(ds.Tables[0].DefaultView.RowFilter = "SNo ='"+compare+"'")
{}





行过滤器将检查所有值中的值行,不仅在行中提供了索引索引(行[0] [0])



Row filter will check the value in all the row, not only in row whoes index is provided (Rows[0][0])




这是自定义大..yy


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

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