string connectionString = "server=localhost;database=mydatabase;user id=myuser;password=mypassword;"; string queryString = "SELECT * FROM mytable;"; DataTable dataTable = new DataTable(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(queryString, connection)) { using (MySqlDataAdapter adapter = new MySqlDataAdapter(command)) { adapter.Fill(dataTable); } } }
string connectionString = "server=localhost;database=mydatabase;user id=myuser;password=mypassword;"; string queryString1 = "SELECT * FROM mytable1;"; string queryString2 = "SELECT * FROM mytable2;"; DataSet dataSet = new DataSet(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command1 = new MySqlCommand(queryString1, connection)) { using (MySqlCommand command2 = new MySqlCommand(queryString2, connection)) { using (MySqlDataAdapter adapter1 = new MySqlDataAdapter(command1)) { using (MySqlDataAdapter adapter2 = new MySqlDataAdapter(command2)) { adapter1.Fill(dataSet, "MyTable1"); adapter2.Fill(dataSet, "MyTable2"); } } } } }This code connects to a MySQL database using a connection string, creates two SQL query strings, creates a new DataSet, creates a MySqlConnection, two MySqlCommand, and two MySqlDataAdapter objects, and then fills the DataSet with data from two database tables using the adapters' Fill methods. The second argument to the Fill method specifies the name of the DataTable that will be created in the DataSet.