现在我有一个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”消息框。
尝试用'重复键集ID = Id' – AmazingDreams 2013-02-20 15:20:23
什么是计数器设置?你怎么知道id的位置与集合中的行相同?另外,我怀疑有一个异常会被抛出,而空的catch会阻止你看到任何东西。你应该使用调试器来验证。 – 2013-02-20 15:30:15
counter是一个全局变量,我用它来保持集合数组/大小的计数。 – Nick 2013-02-20 15:39:59