1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
int retour = -1;
DbTransaction trans = null;
try
{
// Ouverture Connexion
CremecFactory myCremec = new CremecFactory();
_myConnect = (DbConnection)myCremec.GetConnection();
_myConnect.Open();
trans = _myConnect.BeginTransaction();
// Construction de la requete
DbCommand sqlCmd = _myConnect.CreateCommand();
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO nomtable (log_in, pass_word) VALUES ('@log_in', '@pass_word')";
sqlCmd.Parameters.Add(new SqlParameter("@log_in", log_in));
sqlCmd.Parameters.Add(new SqlParameter("@pass_word", pass_word));
// Association cmd/transaction
sqlCmd.Transaction = trans;
sqlCmd.ExecuteNonQuery();
// Retour ID ajouté
DbCommand sqlCmdRet = _myConnect.CreateCommand();
sqlCmdRet.CommandType = CommandType.Text;
sqlCmdRet.CommandText = "select currval('nomtable_seq')";
sqlCmdRet.Transaction = trans;
Int64 res = (Int64)sqlCmdRet.ExecuteScalar();
retour = (int)res ;
trans.Commit();
}
catch (Exception err)
{
if (trans != null)
{
retour = -2;
trans.Rollback();
Trace.Write(err.Source);
}
}
finally
{
try
{
_myConnect.Close();
}
catch (Exception err)
{
}
}
return retour; |
Partager