如何从数据集中读取数据. [英] How to read data from a Dataset.
问题描述
我正在使用Visual Studio Professional 2015,并且想从SQL Server表中读取数据.我能够创建一个数据集,但是正在努力从数据集中读取数据.我不知道从哪里开始(或初始化). 您能否在下一步中帮助我,我应该怎么做才能读取数据并在视图中显示它们.
I am using Visual Studio Professional 2015 and would like to read data from SQL Server table. I was able to create a DataSet but am struggling with reading the data from the dataset. I don;t know where to start(or initialize). Could you please help me with next steps what I should be doing to read the data and display it in a view.
我正在使用MVC框架.
关于
Anand
推荐答案
DataSet 类似于数据库. DataTable 类似于数据库表,并且 DataRow 类似于表中的记录.如果要添加过滤或排序选项,请使用 DataView 对象,然后将其转换回单独的 DataTable 对象.
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.
如果您使用数据库存储数据,则首先将数据库表加载到 内存中的 DataSet 对象.您可以将多个数据库表加载到一个 数据集,然后选择要从 DataSet 中读取的特定表 DataTable 对象.随后,您从 DataTable 到 DataRow .以下代码演示了步骤:
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();
这篇关于如何从数据集中读取数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!