0

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;
    }

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.