使用From子句与OleDbDataAdapter的系统错误

时间:2016-10-11 11:18:56

标签: c# sql ms-access

使用adapter它可以正常工作,但当我使用adapter2时,我收到错误。

注意:代码适用于form1。我正在使用两个groupboxes和两个datagridviews。

   `private OleDbConnection connection = new OleDbConnection();
    DataTable table = new DataTable();
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    DataSet set = new DataSet();

    public Home()
    {
        InitializeComponent();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\DatabesSystem.accdb;Persist Security Info=False;";
        adapter1();
        adapter2();
    }

    private void adapter1()
    {
        try
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter("select CID, Firstname, Middlename, Lastname, Address, Contact_Number, Email from Clients", connection);
            adapter.Fill(set, "Clients");
            table = set.Tables["Clients"];
            dataGridView1.DataSource = table;
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void adapter2()
    {
        try
        {
            OleDbDataAdapter adapter2 = new OleDbDataAdapter("select Transaction_Number, Client_Name, Unit_Name, Full_Price, Down_Payment, Monthly_Payment, Remaning_Balance from Transaction", connection);
            adapter2.Fill(set, "Transaction");
            table = set.Tables["Transaction"];
            dataGridView2.DataSource = table;
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }`

1 个答案:

答案 0 :(得分:3)

Transaction是SQL中的保留关键字。尝试用括号写表名:

OleDbDataAdapter adapter2 = new OleDbDataAdapter("select Transaction_Number, Client_Name, Unit_Name, Full_Price, Down_Payment, Monthly_Payment, Remaning_Balance from [Transaction]", connection);

或者将您的表名重命名为另一个。