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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
public int archiverBDD(String typeFichier, String tableP, String table1, String table2, String table3, String table4, String nomSour, String nomDest, String nbMoisRetention)
{
SqlConnection connexionSQL = null;
SqlTransaction tn = null;
SqlCommand myCommand;
Boolean tracerSQL = true;
int retour = 0;
//1--création de la connexion SQL-------------------------------
try{connexionSQL = new SqlConnection(this.ChaineConnexion_s);}
catch (Exception e)
{
retour = 1;
Tracelog.TracerAlerte("AccesBase.archiverBDD001 erreur lors de la création de la connexion SQL ", e);
}
if (retour == 0)
{
//2--ouverture de la connexion sql---------------------------
try{connexionSQL.Open();}
catch (Exception e)
{
retour = 1;
Tracelog.TracerAlerte("AccesBase.archiverBDD002 erreur lors de l'ouverture de la connexion SQL ", e);
}
if (retour == 0)
{
//3--ouverture de la transaction pour pouvoir effectuer un ROllBack sur tout le fichier------
try{tn = connexionSQL.BeginTransaction("TansactionArchivageCCPV_archive");}
catch (Exception e)
{
retour = 1;
Tracelog.TracerAlerte("listeglobale.InsererBase003 erreur lors de l'ouverture de la Transaction SQL ", e);
}
//4--traitement de l'archivage-------------------------------
if (retour == 0)
{
String sql = "";
String identifiant = "";
int retesterCreationVue = 0;
DateTime dt = DateTime.Now;
int i = Convert.ToInt16("-" + nbMoisRetention);
//TimeSpan ts = new TimeSpan(30 * Convert.ToInt16(nbMoisRetention), 0, 0, 0, 0);
dt=dt.AddMonths(i);
String s_d = Outils.formaterDateBDD(dt);
//4.1 création de la vue temporaire permettant d'identifier les enregistrements à transférer
sql = "CREATE VIEW archivageCCPV_tmp as ";
if (typeFichier == Constantes.trtCC)
{
identifiant = "IDCC";
sql += "select distinct " + identifiant + ", Fichier_NumCP, FV_FVNum, FV_DtFin ";
sql += "from " + tableP + nomSour + " ";
sql += "where FV_DtFin < CONVERT(DATETIME,'" + s_d + "',103)";
}
else
{
identifiant = "IDPV";
sql += "select distinct " + identifiant + ", DtHInfraction ";
sql += "from " + tableP + nomSour + " ";
sql += "where DtHInfraction < CONVERT(DATETIME,'" + s_d + "',103)";
}
try
{
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
catch
{
retesterCreationVue = 1;
}
if (retesterCreationVue == 1)
{
try
{
//la vue existe peut être déjà! on la supprime pour la créer de nouveau
myCommand = new SqlCommand("drop view archivageCCPV_tmp", connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
catch (Exception e)
{
retour = 1;
Tracelog.TracerAlerte("listeglobale.InsererBase004 création de la vue impossible ", e);
Tracelog.TracerAlerte("sql -" + sql + "-", null);
}
}
if(retour==0)
{
try
{
sql = "";
//4.2 déplacement des enregistrements dans la table cible
if (nomDest != "")
{
sql = "insert into " + tableP + nomDest + " ";
sql += " select * from " + tableP + nomSour;
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
sql = "";
if (table1 != "")
{
sql = "insert into " + table1 + nomDest + " ";
sql += " select * from " + table1 + nomSour;
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table2 != "")
{
sql = "insert into " + table2 + nomDest + " ";
sql += " select * from " + table2 + nomSour;
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table3 != "")
{
sql = "insert into " + table3 + nomDest + " ";
sql += " select * from " + table3 + nomSour;
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table4 != "")
{
sql = "insert into " + table4 + nomDest + " ";
sql += " select * from " + table4 + nomSour;
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
}
//tn.Commit();
//tn = connexionSQL.BeginTransaction("TansactionArchivageCCPV_archive");
sql = "";
//4.3 purge des tables sources
sql = "delete from " + tableP + nomSour + " ";
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
sql = "";
if (table1 != "")
{
sql = "delete from " + table1 + nomSour + " ";
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table2 != "")
{
sql = "delete from " + table2 + nomSour + " ";
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table3 != "")
{
sql = "delete from " + table3 + nomSour + " ";
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
sql = "";
if (table4 != "")
{
sql = "delete from " + table4 + nomSour + " ";
sql += " where " + identifiant + " IN (select " + identifiant + " from archivageCCPV_tmp)";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
//4.4 suppression de la vue temporaire
sql = "drop view archivageCCPV_tmp";
myCommand = new SqlCommand(sql, connexionSQL, tn);
myCommand.ExecuteNonQuery();
if (tracerSQL)
Tracelog.TracerLog(sql);
}
catch (Exception e)
{
retour = 1;
tn.Rollback();
Tracelog.TracerAlerte("listeglobale.InsererBase005 une erreur s est produit pendant l archivage ", e);
Tracelog.TracerAlerte("sql -" + sql + "-", null);
}
// si les ajouts en base se sont bien passé on effectue un commit de transaction
if (retour == 0)
{
tn.Commit();
}
}
}
}
}
try
{
//on ferme la connexion sql
connexionSQL.Close();
tn.Dispose();
connexionSQL.Dispose();
}
catch (Exception e)
{
retour = 1;
Tracelog.TracerAlerte("listeglobale.InsererBase010 erreur lors de la suppression des objets SQL et Transaction ", e);
}
return retour;
} |
Partager