新文章

2012年11月29日 星期四

[C#]如何複製資料庫A的資料表到資料庫B的資料表

這裡以SQLite為例,若是SQLServer也適用照著做就可以了!





        /// <summary>
        /// 更新對應資料表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateDBTable(string tableName)
        {
            try
            {
                //來源資料庫
                var Src_sqlite_conn = new SQLiteConnection(@"Data source=" + m_DBPath);
                var Src_sqlAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, Src_sqlite_conn);
                DataTable table = new DataTable();
                Src_sqlAdapter.AcceptChangesDuringFill = false;
                Src_sqlAdapter.Fill(table);
                //目的資料庫
                var Des_sqlite_conn = new SQLiteConnection(@"Data source=management.db3");
                var Des_sqlAdapter = new SQLiteDataAdapter("SELECT * FROM "+tableName, Des_sqlite_conn);
                var cmdBuilder = new SQLiteCommandBuilder(Des_sqlAdapter);
                Des_sqlAdapter.Update(table);
            }
            catch (Exception ex)
            {
            }
        }

沒有留言:

張貼留言