开发者

Access to MS Access with C#

I've to retrieve information of an existing 开发者_Go百科system which is using MS Access( :'( ).

So I added an existing Item(the mdb) to the project, and it created me a DataSet corresponding to all tables I've.

Once I've done this, I try to access to these data:

        ClsDataSet dataSet = new ClsDataSet();

        foreach (ClsDataSet.DOCDOCUMENTSRow docdocumentsRow in dataSet.DOCDOCUMENTS)
        {
            System.Console.WriteLine(docdocumentsRow.nom_document+"-->"+docdocumentsRow.nom_fichier);
        }
        System.Console.ReadLine();

Only to test...

But it doesn't enter in the foreach, it seems it thinks that the table is empty? I directly step on the ReadLine();

So did I missed something? I don't have any exception..

Should I load the table or something like this?

Thank you!


In the snippet you have posted there isn't any TableAdapter that fills your schemas in the dataset you have created. DataSet is just a representation of what you have on db-side, so it is only a structure of db tables and doesn't directly contain data.

To fill your schema with database records you have to call Fill method of the associated TableAdapter. Usually it is created automatically by Visual Studio Designer when you drag-n-drop some database table in a DataSet schema (.xsd).


Just creating the data set using the constructor does not yet retrieve the data. You need code to fill (=> select the rows from the MDB file and store them within your data set instance) the data set first.

The easiest would be to create an ODBC connection to the MDB file and use OdbcConnection and respective OdbsCommand instances to create the actual connection and "SELECT" statements.

EDIT
You may be able to connect to the MDB file directly using the Table Adapter designer. You would then not need to create an ODBC connection.


Here is a tutorial about connecting to an Access DB - http://www.homeandlearn.co.uk/csharp/csharp_s12p4.html. There is a link at the bottom to another page on that site on how to retrieve data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜