Comparing tables in DB2 and SQL Server with C#

cygmwpex  于 2023-10-15  发布在  DB2
关注(0)|答案(1)|浏览(140)

I want to compare the stock records in the IBM DB2 database with the table in the SQL database and import the different records into a dataview. Then I will transfer different records to the SQL table.

I am currently getting the records I want from the db2 stock table, but I could not find how to compare them with the SQL stock table. I think I need to assign a field to the db2 virtual stock table. I want to compare the stock codes and get different records.

I will also put a check at the beginning of different records and transfer them to the SQL stock table.

public partial class frmStok : MetroFramework.Forms.MetroForm
 {
     DB2Connection db2bagla = new DB2Connection(frmMain.settings.db2con);
     SqlConnection sqlbagla = new SqlConnection(frmMain.settings.sqlcon);
     public frmStok()
     {
         InitializeComponent();
     }
     void db2StokGetir()
     {
         try
         {
             DB2DataAdapter db2da = new DB2DataAdapter("SELECT DISTINCT S.PARCAKOD,P.ADI,P.BIRIM,P.KDVYUZ,P.GRUP1 FROM DB2ADMIN.T0020031 S LEFT OUTER JOIN DB2ADMIN.T0050030 P ON S.PARCAKOD = P.KOD WHERE ((YEAR( S.TARIH) >= 2023)) GROUP BY S.PARCAKOD,P.ADI,P.BIRIM,P.KDVYUZ,P.GRUP1", db2bagla);
             SqlDataAdapter sqlda = new SqlDataAdapter("SELECT STOK_KODU FROM TBLSTSABIT", sqlbagla);

             DataSet db2stok = new DataSet();
             DataSet sqlstok = new DataSet();
             db2da.Fill(db2stok, "DMSSTOK");
             sqlda.Fill(sqlstok, "SQLSTOK");
             metroGrid1.DataSource = db2stok.Tables[0];
            

             metroLabel1.Text=metroGrid1.RowCount.ToString();
             
         }
             catch (Exception ex)
         {
             MessageBox.Show(ex.Message.ToString());
         }

     }
7fyelxc5

7fyelxc51#

I suggest you use LINQ after you fill the dataset.

Make sure you the LINQ reference:

using System.Linq;

Here's a sample

var db2stokQry = db2stok.Tables[0].AsEnumerable();
var sqlstokQty = sqlstok.Tables[0].AsEnumerable();

return (from x in db2stokQry
        join y in sqlstok on x.PARCAKOD equals y.STOK_KODU
        select new ClassObject
        {
            prop1 = x.Field1,
            prop2 = y.Field1,
            prop3 = x.Field2
        }).ToList();

相关问题