none
合併 XML 檔,重新塞回sqlite 資料庫資料重覆的問題 RRS feed

  • 問題

  • 我先讀取一個XML 檔後,把它傳進資料庫,可以正常傳,第二次後就會不行(除了原有資料外,加了一些資料),我猜是 資料重覆的問題,但我試了一下去更改 XmlReadMode 與 DataSet.clone and copy 這些東西,似乎它沒有辦法主動去過濾掉重覆的東西,請問有什麼好法子呀! = = 然道真的要先下 sql 語法,然後去把第二次有用的資料取出來,再塞回資料庫嗎?

     

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data.Common;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Data.SQLite;
    using System.Net.Sockets;
    using System.Xml;
    using System.IO;
    using System.Net;
    using System.Diagnostics;

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

            private void Form1_Load(object sender, EventArgs e)
            {
                test();
            }
         
            private void test() {
                try
                {
                    String db = "Data Source=d:\\xxx.db";
                    SQLiteConnection sqlconn = new SQLiteConnection(db);
                    if (!sqlconn.State.ToString().Equals("Open"))
                        sqlconn.Open();

     

                    DbTransaction trans = sqlconn.BeginTransaction();

                    DataSet dataSet = new DataSet();
                    dataSet.AcceptChanges();
                    string CommandText = "SELECT * FROM " + "Equipment";
                    SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter  (CommandText,sqlconn);
                    SQLiteCommandBuilder builder = new SQLiteCommandBuilder(dataAdapter);                                               
                    dataAdapter.FillSchema(dataSet, SchemaType.Source, "Equipment");
                    dataAdapter.Fill(dataSet, "Equipment");               
                    dataSet.WriteXml("D:\\a.xml");     //資料庫原本資料

                   

                    DataTable tb = dataSet.Tables[0];
                    DataColumn[] dcc ={ dataSet.Tables[0].Columns[0] };
                    dataSet.Tables[0].PrimaryKey = dcc;

                    //DataSet target = dataSet.Clone();
                    //DataSet target = dataSet.Copy();   
                    DataSet target = new DataSet();
                    target.AcceptChanges();
                    XmlTextReader reader = new XmlTextReader("d:\\X.xml");               
                    target.ReadXml(reader);
                    target.WriteXml("D:\\b.xml");            //讀到的資料               

                    dataSet.Merge(target, false);
                    dataSet.WriteXml("D:\\c.xml");   //合併後的資料                try
                    {
                        dataAdapter.Update(target, "Equipment");      
                        trans.Commit();
                    }
                    catch
                    {
                        MessageBox.Show("Exception Rollback!");
                        trans.Rollback();
                        throw;
                    }
                    target.Dispose();
                    dataSet.Dispose();
                    trans.Dispose();
                    sqlconn.Close();
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(ee.TargetSite.ToString());
                }
            }
        }
    }

     

    2009年1月21日 上午 06:18

解答