2

I would like to bind a DataGridView to the results of a query generated as text at runtime.

How can I send a query as text to Microsoft Access and bind the results to the DataGridView?

When the user clicks the button (ok_btn), I want the contents of the textbox (query_txt.Text) sent to Microsoft Access, then I want the results of the query shown in my DataGridView (results_grid).

Simple one-row queries are fine for now: (SELECT "a";, SELECT "a", "b";, SELECT now();)

NOTE: C# accepted, VB.NET preferred

1 Answer 1

8
using System.Data.OleDb;

OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;User Id=;Password=;Data Source=" + fileName);
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query_txt.Text, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView.DataSource = ds.Tables[0];
conn.Close();

Note: query_txt.Text is the Query that you want to run. fileName is the path to where your file is stored.

Sign up to request clarification or add additional context in comments.

1 Comment

After updating my Access 2007 connection string with Provider = Microsoft.ACE.OLEDB.12.0. It worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.