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?
1 Answer
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();
2 Comments
npinti
The MySQL connector for .NET can be downloaded here: dev.mysql.com/doc/refman/5.0/en/connector-net.html
Joel Mueller
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.