0

我使用 C# 4.0 和 SQL server 2008 R2 我有一个像这样的 C# 脚本:

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();

它运行良好,但我在调试时错过了 oItem.Date_Clot_Reel 的毫秒值,我得到了 '2013-03-19 16:02:18.807'

但是在我运行这个 sql 之后,我得到了 '2013-03-19 16:02:18.000'

我怎么能错过毫秒807?

提前谢谢你

PS:也许当我使用它时效果很好

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

但是当我这样使用时,我无法使用 sSql += " UNION ALL ";

4

2 回答 2

0

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

请注意日期时间的准确性是百分之一秒(相当于 3.33 毫秒或 0.00333 秒)

小日期时间精度为一分钟

于 2013-03-19T15:20:42.150 回答
0

检查 SQL Server 中的数据类型。要以毫秒为单位存储日期时间,需要采用 DateTime 数据类型而不是 smalldatetime。

于 2013-03-19T15:35:03.623 回答