简体   繁体   中英

System Error using From Clause with OleDbDataAdapter

Using adapter it works fine but when I use adapter2 I get an error.

Note: The code is for form1 . I am using two groupboxes and two 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);
        }
    }`

Transaction is a reserved key word in SQL. Try to write the table name in brackets:

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

Or rename your table name in another one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM