我想将xml文件中的数据与数据库中的数据进行比较 [英] i want to compare data in xml file with data in database

查看:58
本文介绍了我想将xml文件中的数据与数据库中的数据进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xml文件中的数据与数据库中的数据进行比较.请帮帮我.
这是我的代码:

I want to compare data in xml file with data in database. Please help me.
here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.Reflection;
using System.CodeDom;
using System.Collections.Specialized;

namespace WindowsFormsApplication8try
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

       
             private void button1_Click(object sender, EventArgs e)
        {
            XmlWriter writer = XmlWriter.Create(@"C:\SIEM\xml.xml");

            writer.WriteStartElement("QUERY");

            writer.WriteStartElement("IT");
            writer.WriteAttributeString("macName", textBox1.Text);
            writer.WriteElementString("Generated_source", textBox2.Text);
            writer.WriteElementString("TimeStemp", textBox3.Text);
            writer.WriteEndElement();
            
      writer.Close();}

             private void button2_Click(object sender, EventArgs e)
             {

                 string myXMLfile = @"C:\SIEM\xml.xml";
                 DataSet ds = new DataSet();
                 // Create new FileStream with which to read the schema.
                 System.IO.FileStream fsReadXml = new System.IO.FileStream
                     (myXMLfile, System.IO.FileMode.Open);
                 try
                 {
                     //nchar(10) Source;

                     //string Machine_Name = null;

                     //double Time_stemp = 0;
                     //DataSet ds = new DataSet();

                     ds.ReadXml(fsReadXml);
                     DataTable dbTable1 = ds.Tables[0];
                     connString = 
                     DataTable dt = new DataTable("query_table");

DataColumn col1 = new DataColumn("Machine_Name");

DataColumn col2 = new DataColumn("Source_generated");

DataColumn col3 = new DataColumn("Time_stemp");


dt.Columns.Add(col1);

dt.Columns.Add(col2);

ds.Tables.Add(dt);

//Add Employee Data to the table

//Create a new row and add the data to the corresponding columns

//DataRow dr; dr = dt.NewRow();

//dr ["Name"] = "Rajesh";

//dr ["Address"] = "Hagley Road";

//dt.Rows.Add(dr);

//dr = dt.NewRow();

//dr ["Name"] = "Tim Easton";

//dr ["Address"] = "London";

//dt.Rows.Add(dr);

//dr = dt.NewRow();

//dr ["Name"] = "Sara";

//dr ["Address"] = "Oxford";

//// Now add the datarows to the datatable

//dt.Rows.Add(dr);

// After adding the rows to the datatable use AcceptChanges method if there are any changes

ds.AcceptChanges();
                     dataGridView1.DataSource = dbTable1;
                    //ds.ReadXml("Xml.xml");
                     string myConnectionString ="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Documents\Downloads\WindowsFormsApplication8try\Database1.mdf;Integrated Security=True;User Instance=True";
                      SqlConnection myConnection = new SqlConnection(myConnectionString);
                     
                     string query = "insert into query_table values(" + Machine_Name + ",''" + Source + "''," + Time_stemp + ")";
                     DataAdapter objAdapter = new DataAdapter(query,myConnectionString);
                    
                     objAdapter.Fill(ds);

                     //          for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

                     //{

                     //    Machine_Name = ds.Tables[0].Rows[i].ItemArray[0];

                     //    Source = ds.Tables[0].Rows[i].ItemArray[1].ToString();

                     //   Timestemp = ds.Tables[0].Rows[i].ItemArray[2];
                    // DataTable query_table = new DataTable();
                     

                     // string sqlcommand = "insert into query_table values(" + Machine_Name + ",''" + Source + "''," + Time_stemp + ")";

                    // sqlcommand = new SqlCommand(sqlcommand, connection);

                    //objAdapter.InsertCommand = command;

                    // query_tableTableAdapter.InsertCommand.ExecuteNonQuery();



                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.ToString());
                 }
                 finally
                 {
                     fsReadXml.Close();
                 }
             }
                 
             
             private void Form1_Load(object sender, EventArgs e)
             {
                 System.Data.SqlClient.SqlDataAdapter query_tableTableAdapter = new SqlDataAdapter();
                 //System.Data.SqlClient.SqlDataAdapter query_tableTableAdapter = new SqlDataAdapter();
                 //  This line of code loads data into the ''database1DataSet.query_table'' table.
                 query_tableTableAdapter.Fill(this.database1DataSet1.query_table);
                 // This line of code loads data into the ''dataSet1.Credit'' table. 

                // query_tableTableAdapter.Fill(this.database1DataSet1.query_table);


             }

        
    }}



我知道它写得不好,但是我的最初问题是:它没有将我的xml数据保存在数据库中,然后进行进一步的工作



i know it isn''t well written but my initial problem is:it not save my xml data in database,then its further work

推荐答案

是,

1.在数据集中读取名为dataset1的XML文件. 2.从数据库中获取数据到名为dataset2的DataSet中. 3.现在比较两个数据集dataset1 dataset2.您需要使用一个循环,然后遍历每个DataSet中的每一行并找到不同之处.

如果您不知道如何将XML文件读入DataSet,请参考以下链接.

示例1 [示例2 [
All you have to do is,

1. Read the XML file in DataSet called dataset1.
2. Fetch data from the database into DataSet called dataset2.
3. Now compare the two DataSet, dataset1 and dataset2. You need to use a loop and then go through each rows in each DataSet and find the difference.

If you dont know how to read a XML file into DataSet, then refer to the following links.

Example 1[^]

Example 2[^]


这篇关于我想将xml文件中的数据与数据库中的数据进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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