简体   繁体   中英

c# Data from Access won't show on datagridview

I'm new to C# and I'm struggling to make data from Access to show on datagridview. So I made a database connection and it works fine. But the button to load data got an error that seems to be saying that there's a problem on the line "connection.Open();"

        try
        {

            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select Title, Director * from MVDBtable";
            command.CommandText = query;

            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;


            connection.Close();
        }

        //show error message 
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
    }

You must use correct syntax:

string query = "select Title, Director, * from MVDBtable";

or, more likely:

string query = "select Title, Director from MVDBtable";

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