Live Chat Icon For mobile
Live Chat Icon

How do I bind a mdb file to a datagrid

Platform: WinForms| Category: Data Binding

You can use the classes in the System.Data.OleDb namespace to read a MDB file into a datagrid. You instantiate a OleDbConnection object using a connection string to your MDB file. You then instantiate a OleDbDataAdapter that uses this connection object and a SQL query. Next you create a DataSet object, use the OleDbDataAdapter to fill this dataset, and finally attached this dataset to your datagrid. Here is the code that does this.

	private void Form1_Load(object sender, System.EventArgs e)
	{
		// Set the connection and sql strings
		// assumes your mdb file is in your root
		string connString = @'Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb';
		string sqlString = 'SELECT * FROM customers';

		// Connection object
		OleDbConnection connection = new OleDbConnection(connString);

		// Create data adapter object
		OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlString, connection);

		// Create a dataset object and fill with data using data adapter’s Fill method
		DataSet dataSet = new DataSet();
		dataAdapter.Fill(dataSet, 'customers');
			
		// Attach dataset’s DefaultView to the datagrid control
		dataGrid1.DataSource = dataSet.Tables['customers'].DefaultView;
    	}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.