OleDbException:没有定义参数时没有给出参数值

时间:2013-08-13 12:23:50

标签: c# oledb

我在正在开发的应用程序上运行测试时遇到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类的私有属性)。

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

1 个答案:

答案 0 :(得分:5)

当列名不正确时,通常会发生这种情况。确保它们与您从中获取数据的表一致。