0

i m using C# 4.0 and SQL server 2008 R2 i have a C# script like this:

string sSql = "";
foreach (var oItem in LeListVoit_End)
{
    //insert into Lettrvoit 
    if (sSql != "") sSql += " UNION ALL ";
    sSql += "SELECT '" + oItem.IdLettre + "', '" + oItem.FaClient  +"', '" + oItem.Date_Cloture + "', '" + oItem.CodeDest + "', '" + oItem.ModalMode + "', '" + oItem.LibPort + "', '" + oItem.LibExpr + "', '" + oItem.LibUnite + "', '" + oItem.EnlvUnite + "', '" + oItem.NbrColis + "', '" + oItem.Poids.ToString().Replace(',', '.') + "', '" + oItem.LeCR.ToString().Replace(',', '.') + "', '" + oItem.LeVD.ToString().Replace(',', '.') + "', '" + oItem.CodeClient + "', '"
                                            + oItem.RsNom_Exp.Replace("'", " ") + "', '" + oItem.Addr_Exp.Replace("'", " ") + "', '" + oItem.CP_Exp + "', '" + oItem.Ville_Exp.Replace("'", " ") + "', '" + oItem.Tel_Exp + "', '" + oItem.Fax_Exp + "', '"
                                           + oItem.RsNom_Dest.Replace("'", " ") + "', '" + oItem.Addr_Dest.Replace("'", " ") + "', '" + oItem.CP_Dest + "', '" + oItem.Ville_Dest.Replace("'", " ") + "', '" + oItem.Tel_Dest + "', '" + oItem.Fax_Dest + "', '" + oItem.InseeDest + "', '"
                                           + Is_Print + "', '" + CHAUFFEUR + "', '" + oItem.Transporteur + "', '" + oItem.NoOrdreCumul + "', '" + oItem.CodeMag + "', '" + oItem.Facturation + "', '" + oItem.IsLiv_sign + "', '" + oItem.IsLiv_Samedi + "', '" + oItem.Observ + "', '" + oItem.LeAgence + "', '" + oItem.LibTourne + "', '" + oItem.Date_Clot_Reel + "'";
}
string sqlComm_Insert = "INSERT INTO LETTRE_VOIT_FINAL  ([NOID],FA_CLIENT, [DATE_CLOTURE], [CODE_DEST] ,[MODAL_MODE], [LIBELLE_PORT] ,[LIBELLE_EXPR], [LIBELLE_UNITE],ENLEV_UNITE, [NBR_COLIS], [POID], [ENLEV_CREMB], [ENLEV_DECL], CODE_CLIENT, [RS_NOM_EXP] ,[ADDR_EXP]  ,[CP_EXP] ,[VILLE_EXP] ,[TEL_EXP] ,[FAX_EXP],[RS_NOM_DEST] ,[ADDR_DEST] ,[CP_DEST] ,[VILLE_DEST]  ,[TEL_DEST] ,[FAX_DEST],INSEE_DEST, IS_PRINT, CHAUFFEUR, TRANSPORTEUR, NO_ORDRE_CUMMUL, CODE_MAG, FACTURATION, LIVRS_SIGN, LIVRS_SAMD, OBS, LIB_AGENCE, LIB_TOURNE, DATE_CLOTUR_REEL) " + sSql;

SqlCommand comm_Insert = new SqlCommand(sqlComm_Insert, connectionWrapper.conn);
comm_Insert.ExecuteScalar();

it works well but i miss the millesecond value of oItem.Date_Clot_Reel when i debug it, i got '2013-03-19 16:02:18.807'

but after i run this sql i got '2013-03-19 16:02:18.000'

how can i mis the millisecond 807 ?

thanks you in advance

PS: maybe it works fine when i use

comm_Insert.Parameters.AddWithValue("@NDATE_CLOTURE_REEL", oItem.Date_Clot_Reel);

but when i use like this, i can not use sSql += " UNION ALL ";

RB.
  • 36,301
  • 12
  • 91
  • 131
user1958628
  • 409
  • 4
  • 7
  • 18
  • 1
    http://stackoverflow.com/questions/715432/why-is-sql-server-losing-a-millisecond – Th 00 mÄ s Mar 19 '13 at 15:19
  • Please do not use strings to build sql commands like this. You are opening yourself up to SQL injection attacks. – John Koerner Mar 19 '13 at 15:23
  • why don't you Refactor all that mess and create yourself a `Stored Procedure` my goodness.. also Parameterized Queries are your best friend, `Use them` – MethodMan Mar 19 '13 at 15:25
  • i know it is bad, but with union all it is more performance than looping in C# – user1958628 Mar 19 '13 at 15:26
  • Heed your own PS. Use command parameters. Everthing else is really not worth debugging. If you have problems then, ask a good question. – nvoigt Mar 19 '13 at 15:53

2 Answers2

0

http://msdn.microsoft.com/en-us/library/aa258277(v=sql.80).aspx

Pay attantion that datetime accuracy is one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds)

Small date time accuracy is a minute

Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38
0

Check the datatype in SQL Server. To store datetime in miliseconds its required to take DateTime datatype instead of smalldatetime.

Sandeep Kumar
  • 783
  • 1
  • 5
  • 13