2013-02-20 72 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