我正在Visual Studio中制作一个程序,您可以在其中读取特定格式的excel文件,然后在该程序中将excel文件中的数据转换为另一格式,并将其存储在数据库表中。
下面您可以找到我的代码的一部分,其中发生了奇怪的事情
//copy schema into new datatable
DataTable _longDataTable = _library.Clone();
foreach (DataRow drlibrary in _library.Rows)
{
//count number of variables in a row
string check = drlibrary["Check"].ToString();
int varCount = check.Length - check.Replace("{", "").Length;
int count_and = 0;
if (check.Contains("and") || check.Contains("or"))
{
count_and = Regex.Matches(check, "and").Count;
varCount = varCount - count_and;
}
//loop through number of counted variables in order to add rows to long datatable (one row per variable)
for (int i = 1; i <= varCount; i++)
{
var newRow = _longDataTable.NewRow();
newRow.ItemArray = drlibrary.ItemArray;
string j = i.ToString();
//fill variablename with variable number
if (i < 10)
{
newRow["VariableName"] = "Variable0" + j;
}
else
{
newRow["VariableName"] = "Variable" + j;
}
}
}
当varCount
等于1时,在插入Excel文件后运行程序时出现以下错误消息
源不包含DataRows。
我不知道为什么仅一次迭代就无法运行for循环。有人可以帮助我吗?