输入数组长于此表中的列数。) [英] Input array is longer than the number of columns in this table.)

查看:73
本文介绍了输入数组长于此表中的列数。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System。 Threading.Tasks;

使用System.Windows.Forms;

使用Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;

推荐答案

通常,当您发布错误时,您应该提供整个上下文,包括错误发生的确切位置,异常消息和堆栈信息。

In general when you post an error you should provide the entire context including exactly where the error occurred, the exception message and stack information.

在您的情况下,我猜测你在csvData.Rows.Add调用上遇到这个错误。在您之前的foreach循环中,您只在特定列的csvData中创建一个新列。在你的for循环中,你将抓住所有的领域。如果特定的
行有任何列没有与您预期的某个列对齐,那么您将收到错误,因为您传递的字段多于列。

In your case I'm guessing that you're getting this error on the csvData.Rows.Add call. In your earlier foreach loop you are creating a new column in csvData only for specific columns. Later in your for loop you're grabbing all the fields. If the particular row had any columns that didn't line up with one of your expected columns then you'll get the error because you are passing more fields than columns.

要解决此问题,您需要确保使用的列与要检索的字段匹配。你可以通过几种不同的方式做到这一点。选项1是将所有列加载到表中,填充所有字段,然后删除您不关心的列
about。这会减少我暂时加载额外资源的代价。

To resolve this you need to ensure the columns you use match the fields you are retrieving. You can do this a couple of different ways. Option 1 is to load all the columns into the table, populate all the fields and then remove the columns you don't care about. This would be less code I'd wager at the cost of loading extra stuff temporarily.

选项2是只创建你关心的列,就像你现在做的那样然后在for循环稍后显式获取您关心的列,而不是在字段周围使用for循环。然后,您可以确保只读取包含列的字段。
这是我将使用的方法。

Option 2 is to create only the columns you care about as you're doing now and then in the for loop later explicitly fetch the columns you care about instead of using a for loop around the fields. Then you can ensure you only read the fields that have columns. This is the approach I would use.


这篇关于输入数组长于此表中的列数。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆