2

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
  • Well you have a line for the query just like in php but after you have a DataSet then DataFill and finally DataSource, here is a simple sample: windows-programming.suite101.com/article.cfm/… Commented Aug 3, 2010 at 18:23

1 Answer 1

3

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

2 Comments

The MySQL connector for .NET can be downloaded here: dev.mysql.com/doc/refman/5.0/en/connector-net.html
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.

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.