Search code examples
c#datareader

Single DBDataRecord From DataReader


I am using the following generic data helper function to run queries and return them as a collection of DbDataRecords.

public static IEnumerable<DbDataRecord> GetDataRecords(string Query, Dictionary<string, object> Parameters, CommandType CommandType = CommandType.Text)
    {
        using (MySqlConnection Connection = CreateConnection())
        {   
            using (MySqlCommand cmd = new MySqlCommand(Query, Connection))
            {
                cmd.CommandType = CommandType;

                foreach (var Parameter in Parameters)
                {
                    cmd.Parameters.AddWithValue(Parameter.Key, Parameter.Value);
                }

                Connection.Open();     
                using (MySqlDataReader Reader = cmd.ExecuteReader())
                {
                    foreach (DbDataRecord record in Reader)
                    {
                        yield return record;
                    }          
                }                    
                Connection.Close();  
            }
        } 
    }

This works great for multiple results, but I'd also like to create a function that returns a single record from the reader as a single DbDataRecord. What I cant figure out is how to convert a single row from a reader into a DbDataRecord. Here is what I have so far:

 public static DbDataRecord GetDataRecord(string Query, Dictionary<string, object> Parameters, CommandType CommandType = CommandType.Text)
    {
        DbDataRecord Record = null;

        using (MySqlConnection Connection = CreateConnection())
        {
            using (MySqlCommand cmd = new MySqlCommand(Query, Connection))
            {
                cmd.CommandType = CommandType;

                foreach (var Parameter in Parameters)
                {
                    cmd.Parameters.AddWithValue(Parameter.Key, Parameter.Value);
                }

                Connection.Open();
                using (MySqlDataReader Reader = cmd.ExecuteReader())
                {
                   if(Reader.Read() != false)                        
                   {
                       Record = ???;
                   }                        
                }
                Connection.Close();
            }
        }

        return Record;
    }

I have seen lots of examples that show how to return one column (ExecuteScalar), that is not what I am looking for. I also know how to get individual column values. Again, that is not what I am looking to do.

Basically I want to be replace the following foreach loop

foreach (DbDataRecord record in Reader)
{
    yield return record;
}  

with something that converts a single reader row into a DBDataRecord


Solution

  • You could return IDataRecord instead, that could be sufficient, then you can return the MySqlDataReader directly. Or you have to cast it accordingly:

    So this is not possible directly:

    Record = (DbDataRecord)Reader;
    

    but in this way:

    Record = (DbDataRecord)(IDataRecord)Reader;