从DataSet获取值到变量中

kal*_*lls 6 .net c# asp.net ado.net

我有以下方法返回数据集.我使用的是.NET 2.0

DataSet ds = GetAllRecords();
Run Code Online (Sandbox Code Playgroud)

我想从特定行的每列中获取值并将其绑定到变量.怎么能实现这一目标?

目前该方法返回表中的所有行,我必须根据ID找到该特定行.

但是,如果那是不可能的,我可以来

DataSet ds = GetSpecificRecord(id);
Run Code Online (Sandbox Code Playgroud)

但我仍然需要从每列获取值并将其绑定到变量.请指教.

sam*_*ack 7

这将为您提供第 0 行的值,显然您需要修改返回的多行

long id = ds.Tables[0].Rows[0].Field<long>("ID");
Run Code Online (Sandbox Code Playgroud)


Dam*_*ith 6

// Create a table 
DataTable table = new DataTable("users");
table.Columns.Add(new DataColumn("Id", typeof(int)));
table.Columns.Add(new DataColumn("Name", typeof(string)));

table.Rows.Add(1, "abc");
table.Rows.Add(2, "ddd");
table.Rows.Add(3, "fff");
table.Rows.Add(4, "hhh d");
table.Rows.Add(5, "hkf ds");

// Search for given id e.g here 1

DataRow[] result = table.Select("Id = 1"); // this will return one row for above data 
foreach (DataRow row in result)
{
    Console.WriteLine("{0}, {1}", row[0], row[1]);
}
Run Code Online (Sandbox Code Playgroud)