Reputation: 1898
After running a MySqlCommand and then reading a row with MySqlDataReader, is it possible to store that entire row into a single variable?
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// Store a single row into a variable
// Pass the variable into another method
}
Upvotes: 0
Views: 59
Reputation: 216293
The best thing you can achieve is explained in the MSDN docs about the IDataRecord interface, but at the end you still need to read each single column and store them somewhere. However a more object oriented approach is to have a class that represents the data from your record, create an instance of that class and initialize its properties, then pass the instance to another method
So suppose that the query you have used is a simple
SELECT IDPerson, FirstName, LastName from Persons
Now you have defined somewhere your class Person
public class Person
{
public int IDPerson {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
}
Then your loop works with
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// Declare and initialize the instance of a Person
// (this is a single variable as per your requirements)
Person p = new Person()
{
IDPerson = rdr.GetInt32(0),
FirstName = rdr.GetString(1),
LastName = rdr.GetString(2)
};
ProcessPerson(p);
}
Upvotes: 1