Question

I want to make a function wich sends a query to a remote mysql server and puts the data into a DataTable object dynamically, my problem now is to get the values out of the reader and fill that object entirely. If I get 1 row out of the reader its not the whole data, if I get 10 its a out of range error. I dont want to make this bound to a specific table.

    ...
    oCon = new MySqlConnection(...)
    ...        

    private DataTable query(MySqlCommand command, DataTable pattern)
    {
        DataTable table = pattern;
        oCon.Open();
        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Rows.Add(What here?);
            }
        }
        reader.Close();
        oCon.Close();
        return table;
    }
Was it helpful?

Solution

You need to use the GetName function.

MySqlDataReader resultSet = cmd.ExecuteReader();
dt.Columns.Clear();
for (int i = 0; i < resultSet.FieldCount; i++)
{
    dt.Columns.Add(resultSet.GetName(i));
}
dt.Load(resultSet);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top