因为" da.Fill(dt);"而无法填充数据表。不工作请帮助,完成作业

时间:2014-09-02 10:12:20

标签: c# syntax-error

这是我的代码

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;

namespace SDD_Single_Project___Michael
{


    public partial class AllData : Form
    {
        private OleDbConnection connection = new OleDbConnection();

        public AllData()
        {
            InitializeComponent();
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\schoolwork\Year 11\SDD\3 SINGLE TASK\SDD Single Project - Michael \SDD Single Project - Michael \bin\Persondata.accdb;
Persist Security Info=False;";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide(); //hides this page
            MainScreen frm = new MainScreen(); //finds the next screen (the main game)
            frm.Show(); //shows it
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open(); //opens connection 
                OleDbCommand command = new OleDbCommand(); //declare our object of oleDB command
                command.Connection =  connection; // the connection is giving to the command
                string query = "select * Persondata";
                command.CommandText = query; // pass the query to the command

                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt); // the error supposedly occurs here!!
                dataGridView1.DataSource =  dt;


                connection.Close(); // closes the connection
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }


    }
}

我尝试运行程序时得到的错误代码是' * Persondata'在第46行(这是theda.Fill(dt);行),我无法弄明白。请帮忙完成作业。

1 个答案:

答案 0 :(得分:2)

此:

"select * Persondata"

必须是

"select * FROM Persondata"