如何解决' System.Data.OleDb.OleDbException'?

时间:2014-02-03 23:29:30

标签: c# datagridview

首先抱歉告诉我如何解决这个错误(我知道它是一个常见的问题),但我对C#很新,我似乎无法找到解决方案。

我正在创建一个从excel文件导入数据并将其显示在DataGridView中的Windows窗体。执行时我得到错误:

  

“类型'System.Data.OleDb.OleDbException'的未处理异常   发生在System.Data.dll中附加信息:没有给出值   一个或多个必需参数。“

这是我的代码:

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 System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class CurrentOrders : Form
    {
        public CurrentOrders()
        {
            InitializeComponent();
        }

        private void CurrentOrders_Load(object sender, EventArgs e)
        {

        }

        private void BackBtn_Click(object sender, EventArgs e)
        {
            NewOrder NewOrd = new NewOrder();
            this.Hide();
            NewOrd.Show();

        }

        private void DataGridViewLOG_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tombies\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\PCSsheet.xls" + @";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

            OleDbCommand command = new OleDbCommand
            (
                "SELECT DATE, CUSTOMER, PO, COMMENTS, PCS FROM [LOG$]", conn
            );

            DataSet DsOrderLOG = new DataSet();
            OleDbDataAdapter Adapter = new OleDbDataAdapter(command);
            conn.Open();
            Adapter.Fill(DsOrderLOG);
            conn.Close();
            DataGridViewLOG.DataSource = DsOrderLOG.Tables[0];
        }
    }
}

我知道它与底部的'Adapter.Fill'有关,但从那时起我就输了。

感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

Date可能是罪魁祸首。尝试将它(所有其他列名称)放在括号中:

"SELECT [DATE], [CUSTOMER], [PO], [COMMENTS], [PCS] FROM [LOG$]", conn