我不知道为什么会收到上述异常(exception),请有人看一下....
DataTable DataTable_Time = new DataTable("Star_Schema__Dimension_Time");
DataColumn Sowing_Day = new DataColumn();
Sowing_Day.ColumnName = "Sowing_Day";
DataColumn Sowing_Month= new DataColumn();
Sowing_Month.ColumnName = "Sowing_Month";
DataColumn Sowing_Year = new DataColumn();
Sowing_Year.ColumnName = "Sowing_Year";
DataColumn Visit_Day= new DataColumn();
Visit_Day.ColumnName = "Visit_Day";
DataColumn Visit_Month = new DataColumn();
Visit_Month.ColumnName = "Visit_Month";
DataColumn Visit_Year = new DataColumn();
Visit_Year.ColumnName = "Visit_Year";
DataColumn Pesticide_spray_day = new DataColumn();
Pesticide_spray_day.ColumnName = "Pesticide_spray_day";
DataColumn Pesticide_spray_Month = new DataColumn();
Pesticide_spray_Month.ColumnName = "Pesticide_spray_Month";
DataColumn Pesticide_spray_Year = new DataColumn();
Pesticide_spray_Year.ColumnName = "Pesticide_spray_Year";
DataTable_Time.Columns.Add(Pesticide_spray_Year);
DataTable_Time.Columns.Add(Sowing_Day);
DataTable_Time.Columns.Add(Sowing_Month);
DataTable_Time.Columns.Add(Sowing_Year);
DataTable_Time.Columns.Add(Visit_Day);
DataTable_Time.Columns.Add(Visit_Month);
DataTable_Time.Columns.Add(Visit_Year);
DataTable_Time.Columns.Add(Pesticide_spray_day);
DataTable_Time.Columns.Add(Pesticide_spray_Month);
adapter.SelectCommand = new SqlCommand(
"SELECT SowingDate,VisitDate,PesticideSprayDate " +
"FROM Transformed_Table " +
"group by SowingDate,VisitDate,PesticideSprayDate", con);
adapter.SelectCommand.CommandTimeout = 1000;
adapter.Fill(DataSet_DistinctRows, "Star_Schema__Dimension_Time");
DataTable_DistinctRows = DataSet_DistinctRows.Tables["Star_Schema__Dimension_Time"];
int row_number = 0;
int i = 3;
foreach(DataRow row in DataTable_DistinctRows.Rows)
{
DataRow flatTableRow = DataTable_Time.NewRow();
string[] Sarray= Regex.Split(row[0].ToString()," ",RegexOptions.IgnoreCase);
string[] finalsplit = Regex.Split(Sarray[0], "/", RegexOptions.IgnoreCase);
string[] Sarray1 = Regex.Split(row[1].ToString(), " ", RegexOptions.IgnoreCase);
string[] finalsplit2 = Regex.Split(Sarray1[0], "/", RegexOptions.IgnoreCase);
string[] Sarray2= Regex.Split(row[2].ToString(), " ", RegexOptions.IgnoreCase);
string[] finalsplit3 = Regex.Split(Sarray2[0], "/", RegexOptions.IgnoreCase);
flatTableRow["Sowing_Day"] = int.Parse(finalsplit[0]);
flatTableRow["Sowing_Month"] = int.Parse(finalsplit[0]);
flatTableRow["Sowing_Year"] = int.Parse(finalsplit[0]);
flatTableRow["Visit_Day"] = int.Parse(finalsplit2[0]);
flatTableRow["Visit_Month"] = int.Parse(finalsplit2[0]);
flatTableRow["Visit_Year"] = int.Parse(finalsplit2[0]);
flatTableRow["Pesticide_spray_day"] = int.Parse(finalsplit3[0]);
flatTableRow["Pesticide_spray_Month"] = int.Parse(finalsplit3[0]);
flatTableRow["Pesticide_spray_Year"] = int.Parse(finalsplit3[0]);
DataTable_Time.Rows.Add(flatTableRow);
i++;
}
con.Open();
using (SqlBulkCopy s = new SqlBulkCopy(con))
{
s.DestinationTableName = DataTable_Time.TableName;
foreach (var column in DataTable_Time.Columns)
s.ColumnMappings.Add(column.ToString(), column.ToString());
s.BulkCopyTimeout = 500;
s.WriteToServer(DataTable_Time);
}
最佳答案
重要的是要记住,sqlBulkCopy列对于某些版本的SQL区分大小写。我认为是MSSQL 2005。
希望能帮助到你
关于c# - 给定的ColumnMapping与源或目标中的任何列都不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20228359/