Steven Steven - 1 year ago 136 Question

Bind DataGridView To Results of Access SQL Query String

I would like to bind a

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 (
), I want the contents of the textbox (
) sent to Microsoft Access, then I want the results of the query shown in my DataGridView (

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

NOTE: C# accepted, VB.NET preferred

Answer Source
using System.Data.OleDb;

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download