读取数据集数据

2023-09-02 12:01:16 作者:醉一场

这是绝对初学者的问题:如何从在WPF DataSet中读取数据?我只有两列火车计划表,我希望能够读取的发车时间和下一班火车离开计算。例如,现在的时间是12:29我的应用程序应该告诉我,下一班列车将启程12:33。

An absolute beginner's question: how do I read data from a DataSet in WPF? I have a train schedule table with just 2 columns and I want to be able to read the departure times and calculate when the next train is leaving. For example, the time now is 12:29 and my application should tell me that next train will depart at 12:33.

我已经用Google搜索左右。我在.NET 3.5。

I already googled left and right. I'm on .NET 3.5.

感谢

推荐答案

数据集类似的数据库。 数据表类似于数据库表,和的DataRow 类似于在一个表中的记录。如果你想添加过滤和排序选项,你再这样做了数据视图对象,并将其转换回一个单独的数据表的对象。

DataSet resembles database. DataTable resembles database table, and DataRow resembles a record in a table. If you want to add filtering or sorting options, you then do so with a DataView object, and convert it back to a separate DataTable object.

如果您正在使用数据库来存储数据,那么你先装入数据库表到数据集在内存中的对象。您可以加载多个数据库表到一个数据集,然后选择特定的表从数据集到数据表对象读取。随后,您从您的数据表到的DataRow 读取数据的特定行。继codeS演示的步骤:

If you're using database to store your data, then you first load a database table to a DataSet object in memory. You can load multiple database tables to one DataSet, and select specific table to read from the DataSet through DataTable object. Subsequently, you read a specific row of data from your DataTable through DataRow. Following codes demonstrate the steps:

SqlCeDataAdapter da = new SqlCeDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

da.SelectCommand = new SqlCommand(@"SELECT * FROM FooTable", connString);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];

foreach (DataRow dr in dt.Rows)
{
    MessageBox.Show(dr["Column1"].ToString());
}

要读入行特定的单元格:

To read a specific cell in a row:

int rowNum // row number
string columnName = "DepartureTime";  // database table column name
dt.Rows[rowNum][columnName].ToString();
 
精彩推荐
图片推荐