2014-02-18 22 views
0

我有奇怪的問題我想從Oracle(Online Db)中提取記錄並插入並更新到離線的MySQL數據庫如果MYSQL數據庫故障轉移啓動功能。我必須將主要記錄從Oracle更新到MySql,以便如果離線數據庫在遊戲中啓動,則其中包含所有相關的同步數據。後臺工作人員不能使用Oracle連接

我已經寫了BackgroundWorker的類的代碼,這樣我可以在一個單獨的線程 保持以下是代碼,請查看

代碼

using Oracle.DataAccess.Client; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.Data.EntityClient; 
using System.Data; 

namespace HospitalSystem 
{ 
    /// <summary> 
    /// Interaction logic for HospitalSync.xaml 
    /// </summary> 
    public partial class HospitalSync : Window 
    { 
     public BackgroundWorker Worker; 
     public int TotalRecords; 

     public HospitalSync() 
     { 
      InitializeComponent(); 
     } 

     public int CalculatePercentage(int Current) 
     { 
      int Percentage = (int) (Current/TotalRecords) * 100; 
      return Percentage; 
     } 

     public void SyncDataFromKPT(object sender, DoWorkEventArgs e) 
     { 
      MedEntities Db = new MedEntities(); 

      using (OracleConnection connection = new OracleConnection(Db.Database.Connection.ConnectionString)) 
      { 
       DataTable RecordsToUpdateTable = new DataTable(); 
       string SqlCommand = "SELECT * FROM medsec.vu_registration_modify"; 
       using (OracleCommand cmd = new OracleCommand(SqlCommand, connection)) 
       { 
        OracleDataAdapter adapter = new OracleDataAdapter(SqlCommand, connection); 
        adapter.SelectCommand.CommandType = CommandType.Text; 
        adapter.Fill(RecordsToUpdateTable); 
        if (RecordsToUpdateTable.Rows.Count > 0) 
        { 
         for (int j = 0; j <= RecordsToUpdateTable.Rows.Count; j++) 
         { 
          RecCountTotal.Content = j + 1; 
          CalculatePercentage(j); 
         } 

        } 

       } 

      } 

     } 

     public void ProcessCompleted(object sender, ProgressChangedEventArgs e) 
     { 
      UpdateBar.Value = e.ProgressPercentage; 
     } 

     public void InitializeWorker() 
     { 
      Worker = new BackgroundWorker(); 
      Worker.WorkerReportsProgress = true; 
      Worker.DoWork += new DoWorkEventHandler(SyncDataFromKPT); 
      Worker.ProgressChanged += new ProgressChangedEventHandler(ProcessCompleted); 
      Worker.RunWorkerAsync(Worker); 
     } 

     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      InitializeWorker(); 
     } 
    } 
} 

此時MedEntities Db = new MedEntities();雲文件使用後即使我放置斷點,也不會執行代碼。

請指導我如何使代碼功能

+0

您是否收到錯誤訊息? Db.Database.Connection.ConnectionString是否有效?我懷疑Connection屬性需要先設置。你應該嘗試以另一種方式獲取連接字符串(沒有連接字符串,沒有連接,所以你不能使用連接來獲取連接字符串)。 – Markus

+0

我在早先的應用程序中使用相同的邏輯提取了連接字符串,並且它起到了魅力的作用。 –

+0

@Markus當我刪除整個使用塊它執行 –

回答

0

問題爲解決與@Markus的幫助。 謝謝

這是我使用32位客戶端庫並在64位上進行調試的平臺問題。