IM使用C#4.0和SQL Server 2008 R2 我有一個C#這樣的腳本:C#SQL插入與毫秒
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 的millesecond值當我調試它,我得到了 '2013年3月19日16:02:18.807'
但在我運行該SQL我得到了 '2013年3月19日16:02:18.000'
我怎麼能在MIS毫秒807?
感謝您提前
PS:也許它工作正常,當我使用
comm_Insert.Parameters.AddWithValue("@NDATE_CLOTURE_REEL", oItem.Date_Clot_Reel);
但是當我使用這個樣子,我不能使用sSql += " UNION ALL ";
http://stackoverflow.com/questions/715432/why-is-sql-server-losing-a-millisecond – 2013-03-19 15:19:28
請不要使用字符串來構建像這樣的sql命令。你正打開自己的SQL注入攻擊。 – 2013-03-19 15:23:02
爲什麼你不重構所有的混亂,並創建一個'存儲過程'我的天啊..也參數化查詢是你最好的朋友,'使用它們' – MethodMan 2013-03-19 15:25:10