Det handler ikke om at holde forbindelsen åben, du skal bare bruge Command Builder, det er det samme med MySql, tror jeg.
private MySqlDataAdapter adapt;
private DataSet someDataSet;
someDataSet = new DataSet();
public DataSet GetCustomerData(int customerId)
{
using(MySqlConnection connect = new MySqlConnection(ConnString))
{
connect.Open();
MySqlCommand comm = new MySqlCommand("SELECT * FROM customers WHERE Id = @0", connect);
someDataSet.Tables.Add("CustomersTable");
comm.Parameters.AddWithValue("@0", customerId);
adapt.SelectCommand = comm;
adapt.Fill(someDataSet.Tables["CustomersTable"]);
}
return someDataSet;
}
Nu til opdateringen:du kunne også bruge en ny adapter, men så skal du give den en select-kommando, baseret på at kommandobyggeren vil lave kommandoerne Indsæt, Opdater og Slet.
public void UpdateTable(DataTable table, int customerId)
{
using (MySqlConnection connect = new MySqlConnection(ConnString))
{
connect.Open();
MySqlCommandBuilder commbuilder = new MySqlCommandBuilder(adapt);
adapt.SelectCommand = new MySqlCommand("SELECT * FROM customers WHERE Id = "+customerId, connect); //or use parameters.addwithvalue
adapt.Update(table);
}
}