Reading DataSet

后端 未结 4 627
时光说笑
时光说笑 2020-12-12 13:54

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 tra

4条回答
  •  悲&欢浪女
    2020-12-12 14:27

    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.

    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();
    

提交回复
热议问题