tags:

views:

40

answers:

1

I understand that with PHP I can use mysql_query($sql); and mysql_fetch_array($result); to fetch some MySQL data and place it into an array. How is this achieved in C# to where I could place my data in say, a datagrid?

+3  A: 

This is probably the most quintessential ADO.NET code to fill DataGrid you're going to see (using disconnected DataSets, that is):

DataTable results = new DataTable();

using(MySqlConnection conn = new MySqlConnection(connString))
{
    using(MySqlCommand command = new MySqlCommand(sqlQuery, conn))
    {
        MySqlDataAdapter adapter = new MySqlDataAdapter(command);
        conn.Open();
        adapter.Fill(results);
    }
}

someDataGrid.DataSource = results;
someDataGrid.DataBind();
Justin Niessner
+1 nice example.
Prix
The MySQL connector for .NET can be downloaded here:http://dev.mysql.com/doc/refman/5.0/en/connector-net.html
npinti
You can stack the `using` statements on top one one another and have a single block rather than two nested blocks of code. It's functionally identical but (I think) less busy looking.
Joel Mueller
i prefer the way it is imo looks more human-readable.
Prix