简体   繁体   English

.NET-使用DataSet从MS Access DB获取行

[英].NET - get rows from a MS Access DB with DataSet

I have a MS Access database I am trying to access in C# using a DataSet. 我有一个MS Access数据库,我正在尝试使用DataSet在C#中进行访问。 However, while it can get the table's columns, it doesn't see any of its rows. 但是,尽管它可以获取表的列,但看不到其任何行。

Here is the code: 这是代码:

    private void Button_Start_Click(object sender, EventArgs e)
    {
        StringBuilder S = new StringBuilder();

        DataTable TheTable = DataSet_TheData.TheTable;

        DataColumnCollection Cols = TheTable.Columns;

        DataTableReader Rows = TheTable.CreateDataReader();
        while (Rows.Read())
        {
            long Id = Rows.GetInt64(0);
            DateTime StartTime = Rows.GetDateTime(3);
            S.AppendLine(Id.ToString() + " - " + StartTime.ToString("M/d h:mm tt"));
        }
        S.AppendLine("Finished reading data");

        Text_Output.Text = S.ToString();
    }

DataSet_TheTable is a DataSet pointing to an Access database defined in design. DataSet_TheTable是指向设计中定义的Access数据库的DataSet。 Text_Output is a TextBox. Text_Output是一个TextBox。

The table contains five rows. 该表包含五行。
However, Rows.Read always returns false. 但是,Rows.Read始终返回false。 Also, if I try to access DataSet_TheData.TheTable.Rows before the loop, it shows zero rows. 另外,如果我尝试在循环之前访问DataSet_TheData.TheTable.Rows,则它显示零行。 Cols is populated correctly from the Access table, so it's not a case of it not being able to find the table. 从Access表中正确填充了Cols,因此,不是无法找到该表的情况。

Replacing "TheTable.CreateDataReader()" with "new DataTableReader(TheTable)" does not work either. 用“ new DataTableReader(TheTable)”替换“ TheTable.CreateDataReader()”也不起作用。

How do I get the code to find the rows in the table? 如何获取代码以查找表中的行?

To loop through the rows of a DataTable in c# you just need to do: 要遍历c#中DataTable的行,您只需要执行以下操作:

foreach (DataRow myRow in DataSet_TheData.Tables[0].Rows) {
    //Work with myRow here.
}

This will loop through the 5 rows in the DataTable returned to you. 这将遍历返回给您的DataTable中的5行。

Then, on each row you just access the value of a column for a specific row by using: 然后,在每一行上,只需使用以下命令访问特定行的列值:

myRow["myColumnName"]

If you want to do this programatically for each row: 如果要以编程方式对每一行执行此操作:

foreach (DataRow myRow in DataSet_TheData.Tables[0].Rows) {
    foreach (DataColumn myCol in DataSet_TheData.Tables[0].Columns) {
       //Use myRow[myCol] here
    }
}

It appears that Visual Studio doesn't so much as link to the existing Access Database as make an internal empty copy of its tables, which is why the columns showed up but the rows did not. 看起来Visual Studio与其链接到现有的Access数据库,还不如为其表建立一个内部空副本,这就是为什么显示列而不显示行的原因。 When I added this code, the table data was populated: 当我添加此代码时,表数据已填充:

TestAccessDatabaseDataSetTableAdapters.TheTableTableAdapter A =
     new TestAccessDatabaseDataSetTableAdapters.TheTableTableAdapter();
A.Fill(TheDataSet.TheTable);

Boy, that was intuitive... 天哪,这很直观...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM