0

I have configured a accessdb datasource in Microsoft Visual Studio, I have a dataset and a datatable called "Contractor". How can I search this data within a Form? More to the point how can I reference this table in code?

p.s. I've looked everywhere on google about datatables and all the examples show instantiating one locally, adding data and then searching for it not referencing one that already exists. So i have done my due diligence. Need help.

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Aiden Rigby
  • 569
  • 1
  • 5
  • 7
  • Do you have a code sample of what you've tried so far? Are you using `OleDbDataReader`? – grovesNL Nov 09 '14 at 08:09
  • This is what my configuration is: connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Phase2.accdb" providerName="System.Data.OleDb" I made this through the wizard I just want to execute sql on the table "Contractor" in code though not DataGridViews – Aiden Rigby Nov 09 '14 at 08:15
  • That's the connection string. What about code to query (read) data from the database? – Tim Nov 09 '14 at 08:16
  • Yeh thats what i'm asking. How can I reference the tables I just imported. – Aiden Rigby Nov 09 '14 at 08:16
  • @AidenRigby , use - http://stackoverflow.com/questions/2374221/loading-access-db-table-to-datatable , to select data from table existing in file. – Arindam Nayak Nov 09 '14 at 08:38

1 Answers1

0

If you want to query the Database use the oledbCommand and OleDbDataReader

using (System.Data.OleDb.OleDbConnection oledbConnection = new System.Data.OleDb.OleDbConnection(ConnectionString))
            {
                oledbConnection.Open();
                using (System.Data.OleDb.OleDbCommand getData = new System.Data.OleDb.OleDbCommand())
                {
                    getData.CommandText = "select * from table ";
                    getData.Connection = oledbConnection;
                    using (System.Data.OleDb.OleDbDataReader readData = getData.ExecuteReader())
                    {
                        if (readData.HasRows)
                        {
                            while (readData.Read())
                            {
                              // get the data here
                            }
                        }

                    }
                }

if you want to query a datatable use the asEnumerable() extension of datatable

 var readData = from currentRow in theDataTable.AsEnumerable()
 where currentRow.Field<int>("RowNo") == 1
 select currentRow;
MediSoft
  • 163
  • 2
  • 12