Dot-Net
錯誤:<target>.ColumnName 和 <source>.ColumnName 具有衝突的屬性:DataType 屬性不匹配
我正在嘗試使用 DataTable.Merge() 選項合併多個 excel 文件
For Each fileName As String In Directory.GetFiles("C:\TEMP\.", "*.xls") Dim connectionString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;""", fileName) Dim adapter As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString) Dim ds As New DataSet adapter.Fill(ds, "anyNameHere") Dim TempTable As DataTable TempTable = ds.Tables.Item("anyNameHere") table1.Merge(TempTable) MsgBox(fileName) Next DataGridView1.DataSource = table1 MsgBox(table1.Rows.Count)但是在合併時會出現以下錯誤
<target>.ColumnName and <source>.ColumnName have conflicting properties: DataType property mismatch.這是因為 excel 中的一列被讀取為文本,另一列被讀取為雙精度,而兩者都有數值。
為了避免這種情況,我還在連接字元串中提到了 IMEX=1,但仍然出現此錯誤。
在 Merge 中使用 MissingSchemaAction.Ignore 作為 MissingSchemaAction 參數
table1.Merge(TempTable, True, MissingSchemaAction.Ignore)