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
2010-08-03 18:22:35
+1 nice example.
Prix
2010-08-03 18:25:27
The MySQL connector for .NET can be downloaded here:http://dev.mysql.com/doc/refman/5.0/en/connector-net.html
npinti
2010-08-03 18:34:25
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
2010-08-03 18:57:41
i prefer the way it is imo looks more human-readable.
Prix
2010-08-03 21:24:33