2014-09-02 116 views
0

这里是我的代码由于“da.Fill(dt);”无法获取Datatable来填充不工作。请大家帮忙,工作分配

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(这是西达。填充(dt);线),我无法弄清楚。请帮忙完成作业。

+1

请张贴实际的错误信息 – 2014-09-02 10:13:43

+0

查询没有 “从” 关键字,它应该是 “从Persondata选择*”。此外,如果我是正确的,你正在阅读MS Access文件 - 也尝试将表名放入[]([Persondata])。 – NDraskovic 2014-09-02 10:15:45

+4

我很肯定这是在抛出一个异常,我很确定这个异常已经告诉你到底发生了什么问题...... – 2014-09-02 10:16:38

回答

1

此:

"select * Persondata" 

必须

"select * FROM Persondata"