Bonjour,
Ci-dessous mon code.
Le dataAdapter est bien modifié, mais lors du commit aucune modification dans la DB alors qu'aucune exception n'est levée. Commente contrôler la transaction ?
Db sqlserveur express.
Pouvez vous m'aider
Merci.
private void FCAloc_FormClosed(object sender, FormClosedEventArgs e)
{
if(RDataSet.HasChanges()) // Si modification -> Transaction DB
{
SqlConnection RCon = new SqlConnection(RConStr);
string RQueryString = "SELECT * FROM T_CALOC ORDER BY NOM_CALOC";
SqlDataAdapter RAdapter = new SqlDataAdapter(RQueryString, RCon);
try
{
RCon.Open();
RCommandBuilder = new SqlCommandBuilder(RAdapter);
RAdapter.UpdateCommand = RCommandBuilder.GetUpdateCommand();
RAdapter.DeleteCommand = RCommandBuilder.GetDeleteCommand();
RAdapter.InsertCommand = RCommandBuilder.GetInsertCommand();
RTransaction = RCon.BeginTransaction();
RAdapter.UpdateCommand.Transaction = RTransaction;
RAdapter.DeleteCommand.Transaction = RTransaction;
RAdapter.InsertCommand.Transaction = RTransaction;
RAdapter.Update(RDataSet,"T_CALOC");
RTransaction.Commit();
}
catch (Exception RException)
{
MessageBox.Show(RException.ToString(), "Erreur Sauvegarde en DB");
if (RTransaction != null)
RTransaction.Rollback();
}
finally
{
if (RCon != null && RCon.State == ConnectionState.Open)
RCon.Close();
RTransaction.Dispose();
RDataSet.Dispose();
RCommandBuilder.Dispose();
RCon.Dispose();
}
}
}
Partager