0

我在正在开发的应用程序上运行测试时遇到了 OleDbException。给出的错误消息是“没有为一个或多个必需参数提供值。”

我知道这个问题似乎已经在这个网站上被问过好几次了,但在每种情况下,问题都是在执行UPDATE命令时出现的。我的错误正在我的SELECT命令中抛出;更令人困惑的是,我什至没有尝试使用参数,所以我不确定它期望什么参数。引发异常的方法的代码如下:

// Array of column letters, used in constructing the select query.
private readonly string[] COLUMN_LABELS = new string[] { "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB",
                                                        "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL",
                                                        "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV",
                                                        "BW", "BX", "BY", "BZ" }; 



 public AvailabilityReader(string spreadsheet, List<ServiceType> sundayList)
        {
            // Set up a connection to the spreadsheet.
            this.spreadsheet = spreadsheet;
            Connection = new OleDbConnection(string.Format((Spreadsheet.EndsWith(".xlsx") ?
                    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;" :
                    "Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;"), Spreadsheet));

            // Set up an adapter for the spreadsheet.
            ColumnNames = new List<string>();
            var columnString = "[E-mail Address] AS Email";
            for (int i = 1; i <= sundayList.Count; i++)
            {
                columnString += string.Format(", Column{0}", i);
                ColumnNames.Add(string.Format("Column{0}", i));
            }

            var selectCommand = new OleDbCommand(string.Format("SELECT {0} FROM [DATA$O5:{1}300]", columnString, COLUMN_LABELS[sundayList.Count - 1]), Connection);
            Adapter = new OleDbDataAdapter(selectCommand);
            CommandBuilder = new OleDbCommandBuilder(Adapter);
            CommandBuilder.QuotePrefix = "[";
            CommandBuilder.QuoteSuffix = "]";

            // Obtain the table.
            var data = new DataSet();
            Adapter.Fill(data, "DATA");
            table = data.Tables["DATA"];
        }

调用时引发异常Adapter.Fill(data, "DATA")(适配器是AvailabilityReader类的私有属性)。

老实说,我不知道发生了什么,但我完全希望错过一些非常简单的事情。提前致谢!

4

1 回答 1

6

这通常发生在您的列名不正确时。确保它们与您从中获取数据的表一致。

于 2013-08-13T12:29:36.117 回答