日期:2014-05-19  浏览次数:20805 次

怎样用SqlDataReader读取dataSet里的表数据呢
如题,请指点下

------解决方案--------------------
参考
sqlstr = "select * from table1 where id = 1 ";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = sqlstr;
conn.Open();
OleDbDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
string s = reader[ "id "].ToString();
}
conn.Close();

------解决方案--------------------
在ADO.NET 2.0中使用
DataTable.CreateDataReader 方法

private static void TestCreateDataReader(DataTable dt)
{
// Given a DataTable, retrieve a DataTableReader
// allowing access to all the tables ' data:
using (DataTableReader reader = dt.CreateDataReader())
{
do
{
if (!reader.HasRows)
{
Console.WriteLine( "Empty DataTableReader ");
}
else
{
PrintColumns(reader);
}
Console.WriteLine( "======================== ");
} while (reader.NextResult());
}
}

private static DataTable GetCustomers()
{
// Create sample Customers table, in order
// to demonstrate the behavior of the DataTableReader.
DataTable table = new DataTable();

// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add( "ID ", typeof(int));
table.Columns.Add( "Name ", typeof(string));

// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };

table.Rows.Add(new object[] { 1, "Mary " });
table.Rows.Add(new object[] { 2, "Andy " });
table.Rows.Add(new object[] { 3, "Peter " });
table.Rows.Add(new object[] { 4, "Russ " });
return table;
}

private static void PrintColumns(DataTableReader reader)
{
// Loop through all the rows in the DataTableReader
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write(reader[i] + " ");
}
Console.WriteLine();
}
}
------解决方案--------------------
DataSet里的数据可以直接访问

例如

string[] sArr = new string[ds.tables[0].rows.count]
for(int i=0;i <ds.tables[0].rows.count;i++)
{
sArr[i] = ds.tables[0].rows[i][ "id "].tostring();
}
------解决方案--------------------
dataReader.Read方法读取当前表的内容,

dataReader.NextResult读取下一个表的内容,如果存在.