无法在 C# 中删除数据表中的空列名
Unable to remove empty column names in the datatable in C#
我有一个 Excel 业务用户将更新并将其发送给我们。 Blue Prism 机器人将选择文件并上传到 SQL 数据库 table 使用代码阶段(使用代码阶段要快得多)。我已经编写了 C# 代码来执行此操作,但是大多数情况下由于列中的白色 space 而失败。
结果是当代码使用已用范围时它也采用白色 space 并创建列名称为 F16、F17 的数据 table ...(当列数为 15 时)
我想动态地处理这个问题。即不管列数和白色 space 列数,代码应该通过从数据 tables
中删除那些白色 space 列来工作
我已经编写了代码来处理这个问题,但是没有用。它抛出一个错误:
Collection was modified; enumeration operation might not execute [duplicate]
try
{
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", Path);
using (OleDbConnection xlConnection = new OleDbConnection(excelConnectionString))
{
OleDbCommand oleDbCmd = new OleDbCommand ("Select * FROM [" + SheetName + "$]", xlConnection);
xlConnection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dbDtRdr = oleDbCmd.ExecuteReader())
{
//Remove Empty Rows
var dtTable = new DataTable();
dtTable.Load(dbDtRdr);
for (int i = dtTable.Rows.Count - 1; i>=0;i--)
{
if(dtTable.Rows[i][1] == DBNull.Value)
dtTable.Rows[i].Delete();
}
dtTable.AcceptChanges();
//Remove Empty Columns
int z = 1;
foreach(DataRow r in dtTable.Rows)
{
foreach(DataColumn c in dtTable.Columns)
{
if (c.ColumnName=="F" + z.ToString())
dtTable.Columns.Remove(c.ColumnName);
}
}
dtTable.AcceptChanges();
// SQL Server Connection String
string sqlConnectionString = @"Data Source=" + SQLServerName + ";Initial Catalog=" + SQLDBName + ";Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = TableName;
bulkCopy.WriteToServer(dtTable);
}
}
xlConnection.Close();
}
Success = true;
Message = "Success";
}
catch (System.IO.IOException e)
{
Message = e.Message;
Success = false;
}
看来我无法使用上述代码中的删除命令删除数据中的任何列table
如果有人能帮助我,那将是很大的帮助。
注意:我尝试使用 For Next 循环向后重复它,如下所示,但仍然遇到问题
for (int z = dtTable.Columns.Count - 1; z>=0;z--)
{
if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z).ToString())
dtTable.Columns.Remove(dtTable.Columns[z].ColumnName.ToString());
}
dtTable.AcceptChanges();
根据下面给出的 JohnPete22 的评论,它工作正常
If you think about it, you're looping through each DataColumn and then also removing from that Collection at the same time, which cannot happen. You need a For loop, and then loop backwards to 0 (which you can see happening just above that line). General rule of thumb: not a good idea to remove from a collection you're looping through at the same time, unless you work your way backwards
还需要注意以下几点
I have tried using John's solution earlier but didn't work. Then I realised that I have used if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z).ToString()) however it should be if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z+1).ToString()) since the additional white space name using the index of 1 and not 0 for columns.
我有一个 Excel 业务用户将更新并将其发送给我们。 Blue Prism 机器人将选择文件并上传到 SQL 数据库 table 使用代码阶段(使用代码阶段要快得多)。我已经编写了 C# 代码来执行此操作,但是大多数情况下由于列中的白色 space 而失败。
结果是当代码使用已用范围时它也采用白色 space 并创建列名称为 F16、F17 的数据 table ...(当列数为 15 时)
我想动态地处理这个问题。即不管列数和白色 space 列数,代码应该通过从数据 tables
中删除那些白色 space 列来工作我已经编写了代码来处理这个问题,但是没有用。它抛出一个错误:
Collection was modified; enumeration operation might not execute [duplicate]
try
{
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", Path);
using (OleDbConnection xlConnection = new OleDbConnection(excelConnectionString))
{
OleDbCommand oleDbCmd = new OleDbCommand ("Select * FROM [" + SheetName + "$]", xlConnection);
xlConnection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dbDtRdr = oleDbCmd.ExecuteReader())
{
//Remove Empty Rows
var dtTable = new DataTable();
dtTable.Load(dbDtRdr);
for (int i = dtTable.Rows.Count - 1; i>=0;i--)
{
if(dtTable.Rows[i][1] == DBNull.Value)
dtTable.Rows[i].Delete();
}
dtTable.AcceptChanges();
//Remove Empty Columns
int z = 1;
foreach(DataRow r in dtTable.Rows)
{
foreach(DataColumn c in dtTable.Columns)
{
if (c.ColumnName=="F" + z.ToString())
dtTable.Columns.Remove(c.ColumnName);
}
}
dtTable.AcceptChanges();
// SQL Server Connection String
string sqlConnectionString = @"Data Source=" + SQLServerName + ";Initial Catalog=" + SQLDBName + ";Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = TableName;
bulkCopy.WriteToServer(dtTable);
}
}
xlConnection.Close();
}
Success = true;
Message = "Success";
}
catch (System.IO.IOException e)
{
Message = e.Message;
Success = false;
}
看来我无法使用上述代码中的删除命令删除数据中的任何列table
如果有人能帮助我,那将是很大的帮助。
注意:我尝试使用 For Next 循环向后重复它,如下所示,但仍然遇到问题
for (int z = dtTable.Columns.Count - 1; z>=0;z--)
{
if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z).ToString())
dtTable.Columns.Remove(dtTable.Columns[z].ColumnName.ToString());
}
dtTable.AcceptChanges();
根据下面给出的 JohnPete22 的评论,它工作正常
If you think about it, you're looping through each DataColumn and then also removing from that Collection at the same time, which cannot happen. You need a For loop, and then loop backwards to 0 (which you can see happening just above that line). General rule of thumb: not a good idea to remove from a collection you're looping through at the same time, unless you work your way backwards
还需要注意以下几点
I have tried using John's solution earlier but didn't work. Then I realised that I have used if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z).ToString()) however it should be if (dtTable.Columns[z].ColumnName.ToString()=="F" + (z+1).ToString()) since the additional white space name using the index of 1 and not 0 for columns.