我有一個Excel表單,包含4列(JobCode,JobName,StartDate,EndDate)。在一個規則的基礎上,我必須驗證第1張excel表格,並將第2張excel表格中的所有記錄(第1張excel表格中存在的重複記錄除外)全部插入。 我試圖使用列表。但它按預期工作。如何使用oledb刪除excel表單中的重複記錄
List<string> JobCodeList = new List<string>();
for (int iRowCount = 0; iRowCount < hrms_jobdata.Tables[0].Rows.Count; iRowCount++)
{
JobCode = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Code"].ToString();
JobName = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Name"].ToString();
StartDate = hrms_jobdata.Tables[0].Rows[iRowCount]["Start Date"].ToString();
EndDate = hrms_jobdata.Tables[0].Rows[iRowCount]["End Date"].ToString();
JobCodeList.Add(JobCode + JobName);
}
connectionhrms_job.Close();
for (int iRowCount = 0; iRowCount < hrms_jobdata.Tables[0].Rows.Count; iRowCount++)
{
JobCode = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Code"].ToString();
JobName = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Name"].ToString();
StartDate = hrms_jobdata.Tables[0].Rows[iRowCount]["Start Date"].ToString();
EndDate = hrms_jobdata.Tables[0].Rows[iRowCount]["End Date"].ToString();
DateTime convertedstart = DateTime.Parse(StartDate);
StartDateFormated = convertedstart.ToString("dd-MM-yyyy");
DateTime convertedend = DateTime.Parse(EndDate);
EndDateFormated = convertedend.ToString("dd-MM-yyyy");
List<string> dupvalue = removeDuplicates(JobCodeList);
foreach (string value in dupvalue)
{
string jobcodename = value;
}
string connectionStringdest = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathdestination + ";Extended Properties=Excel 12.0;";
DbProviderFactory factorydest = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connectiondest = factorydest.CreateConnection();
connectiondest.ConnectionString = connectionStringdest;
DbCommand command = connectiondest.CreateCommand();
StringBuilder inserthrms_job = new StringBuilder();
inserthrms_job = inserthrms_job.Append("Insert into [hrms_job$] values ('" + JobCode + "', '" + JobName + "', '" + StartDateFormated + "', '" + EndDateFormated + "','" + JobCode + " " + JobName + "') ");
inserthrms_job = inserthrms_job.Append(";");
command.CommandText = inserthrms_job.ToString();
connectiondest.Open();
command.ExecuteNonQuery();
connectiondest.Close();
}
嗯,你的代碼格式的一部分出了問題,我沒有看到你的問題是什麼。你說你想要它做什麼,然後你說列表正在按預期工作......但不是你遇到什麼麻煩。 – 2011-01-11 17:51:03