参考 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(); } }
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();
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();
}
}
for(int i=0;i<ds.tables[0].rows.count;i++)
{
sArr[i] = ds.tables[0].rows[i]["id"].tostring();
}