我正在導入Excelsheet到sql服務器數據庫中,我需要檢查某些東西在導入工作表之前,例如我有一個名爲Passport的列,它必須具有僅以字母開頭的值,並且必須將剩餘的字符作爲數字(僅限於)。如何比較使用SqlBulkCopy時excel單元格值的值?
DataTable dt7 = new DataTable();
dt7.Load(dr);
DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelTable";
dt7.Rows.CopyTo(ExcelRows, 0);
for (int i = 0; i < ExcelRows.Length; i++)
{
if (ExcelRows[i]["data"] == DBNull.Value)
{
// Include any actions to perform if there is no date
//ExcelRows[i]["data"] = Convert.ToString(0);
}
else
{
DateTime oldDate = Convert.ToDateTime(ExcelRows[i]["data"]).Date;
DateTime newDate = Convert.ToDateTime(oldDate).Date;
ExcelRows[i]["data"] = newDate.ToString("yyyy/MM/dd");
}
}
bulkCopy.WriteToServer(ExcelRows);
哪裏是上面代碼中的護照欄? – andy
所以你需要在BCP之前檢查它?使用查詢/ SP在數據庫中執行它可以嗎?因爲使用SQL Server內置函數應該相對簡單。 – OzrenTkalcecKrznaric
@Anandkumar它不是我要添加護照我,我剛剛發佈了這段代碼,以顯示我用什麼方法上傳excel – Arbaaz