2013-02-20 71 views
2

現在我有一個SQL Server表,它填充了外部.CSV文件中的數據,該數據通過附加新數據偶爾進行更新。最初,我的程序設置爲每次運行時都刪除表中的所有內容,並且它會從.CSV文件中上傳所有內容。現在我試圖重新配置它,以便它只從文件中插入新附加的數據,而不是先刪除所有內容。只向SQL中插入新數據表

該程序的工作原理是首先定義上傳到數組的.CSV文件中的數據。然後,我基本上通過數組並上傳每個元素。現在我有輔助數組,包含表中所有當前的主鍵,到目前爲止,我試圖將它們與正在讀取的.CSV文件中的數據進行比較。因此,它會檢查.CSV文件中的主鍵是否已經存在於表/輔助數組中,並執行插入操作。這似乎是一個合理的邏輯,但由於某種原因,我無法實現它的工作。

這裏是我的代碼的肉:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 

     //Create new SQL connection and adapter using Windows Authentication 
     SqlConnection myConnection = new SqlConnection("Data Source=database; Initial Catalog=Dashboard; Integrated Security=SSPI; Persist Security Info=false; Trusted_Connection=Yes"); 
     SqlDataAdapter da = new SqlDataAdapter(); 

     try 
     { 
      myConnection.Open(); 

      da.InsertCommand = new SqlCommand("INSERT INTO DashboardLibAnswer(Id,Date,Time,Question,Details,Answer,Notes,EnteredBy,WhereReceived,QuestionType,AnswerMethod,TransactionDuration)" 
       + "VALUES(@Id,@Date,@Time,@Question,@Details,@Answer,@Notes,@EnteredBy,@WhereReceived,@QuestionType,@AnswerMethod,@TransactionDuration)", myConnection); 

      da.InsertCommand.Parameters.Add("@Id", SqlDbType.NVarChar); 
      da.InsertCommand.Parameters.Add("@Date", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@Time", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@Question", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@Details", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@Answer", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@Notes", SqlDbType.Text); 
      da.InsertCommand.Parameters.Add("@EnteredBy", SqlDbType.NVarChar); 
      da.InsertCommand.Parameters.Add("@WhereReceived", SqlDbType.NVarChar); 
      da.InsertCommand.Parameters.Add("@QuestionType", SqlDbType.NVarChar); 
      da.InsertCommand.Parameters.Add("@AnswerMethod", SqlDbType.NVarChar); 
      da.InsertCommand.Parameters.Add("@TransactionDuration", SqlDbType.NVarChar); 

      //Using the global variable counter this loop will go through each valid entry and insert it into the specifed database/table 
      for (int i = 0; i < counter; i++) 
      { 
       //This is where I try to do the comparision. 
       //idS is the secondary array with all the current primary keys in the Table 
       //collection is the primary array that stores new data from the .CSV file 
       if (idS.ElementAt(i) != collection.getIdItems(i)) 
       { 
        da.InsertCommand.Parameters["@Id"].Value = collection.getIdItems(i); 
        da.InsertCommand.Parameters["@Date"].Value = collection.getDateItems(i); 
        da.InsertCommand.Parameters["@Time"].Value = collection.getTimeItems(i); 
        da.InsertCommand.Parameters["@Question"].Value = collection.getQuestionItems(i); 
        da.InsertCommand.Parameters["@Details"].Value = collection.getDetailsItems(i); 
        da.InsertCommand.Parameters["@Answer"].Value = collection.getAnswerItems(i); 
        da.InsertCommand.Parameters["@Notes"].Value = collection.getNotesItems(i); 
        da.InsertCommand.Parameters["@EnteredBy"].Value = collection.getEnteredByItems(i); 
        da.InsertCommand.Parameters["@WhereReceived"].Value = collection.getWhereItems(i); 
        da.InsertCommand.Parameters["@QuestionType"].Value = collection.getQuestionTypeItems(i); 
        da.InsertCommand.Parameters["@AnswerMethod"].Value = collection.getAnswerMethodItems(i); 
        da.InsertCommand.Parameters["@TransactionDuration"].Value = collection.getTransactionItems(i); 
        da.InsertCommand.ExecuteNonQuery(); 
       } 


       //Updates the progress bar using the i in addition to 1 
       _worker.ReportProgress(i + 1); 

      } // end for 

      //Once the importing is done it will show the appropriate message 
      MessageBox.Show("Finished Importing"); 

     } // end try 
     catch (Exception exceptionError) 
     { 
      //To show exceptions thrown just uncomment bellow line 
      //rtbOutput.AppendText(exceptionError.ToString); 

     } // end catch 

     //Closes the SQL connection after importing is done 
     myConnection.Close(); 

    } // end backgroundWorker1_DoWork 

眼下的程序執行,但沒有新的數據被插入。看起來程序沒有離開for循環,因爲如果它成功運行,它會顯示「Finished Importing」消息框。

+0

嘗試用'重複鍵集ID = Id' – AmazingDreams 2013-02-20 15:20:23

+0

什麼是計數器設置?你怎麼知道id的位置與集合中的行相同?另外,我懷疑有一個異常會被拋出,而空的catch會阻止你看到任何東西。你應該使用調試器來驗證。 – 2013-02-20 15:30:15

+0

counter是一個全局變量,我用它來保持集合數組/大小的計數。 – Nick 2013-02-20 15:39:59

回答

1

if (idS.ElementAt(i) != collection.getIdItems(i)) 

看起來很奇怪。根據getIdItems返回的內容,它在我看來就像是第一次匹配兩個列表不同步 - 所以如果項目沒有以與您已經保存它們的順序完全相同的順序出現,您可能會得到假陽性。 IE瀏覽器。該代碼將評估該項目不存在並且應該被保留,即使它存在於您的收藏中的某個其他位置。

然後,如果你有一個唯一的約束,插入將失敗,將你發送到註釋掉的異常處理程序 - 然後退出。

我會做的第一件事就是激活異常處理程序來知道發生了什麼。其次,我可能會更改上面的代碼,以便查找,而不是遍歷idS集合。 IE瀏覽器。如果你讓idS列表或你有LINQ的擴展方法,你可以這樣做:

if (!idS.Contains(collection.getIdItems(i)) 

編輯:請注意,大idS,這不是特別有效,併爲您的列表變得不規模非常好。如果您希望處理大量的idS,則可能需要將列表更改爲哈希集合或使用此處建議的解決方案Check if list<t> contains any of another list

+0

當我啓用異常時,它返回:「跨線程操作無效:從其創建的線程以外的線程訪問的控制'rtbOutput'」。你也是什麼意思查找? – Nick 2013-02-20 15:37:34

+0

我用一個例子編輯了我的答案。 – 2013-02-20 15:45:32

+0

至於返回的異常,看看http://stackoverflow.com/a/5037487/806549 – 2013-02-20 15:54:06