我想使用准备好的语句将一些数据插入到 SQL Server 数据库中。
我想要的查询是:
INSERT INTO salesdata([OriginalFilename], [InterchangeReceiverID], [Distributor],
[DE_Date], [DE_VendorNR], [DE_VendorDEANR], [DE_VendorName],
[DE_DistributionCenterNR], [DE_DistributionCenterID],
[DE_DistributionCenterName], [DE_ItemNR],[DE_ItemNDCNR], [DE_ProductUOM],
[DE_CustomerAccountNR], [DE_CustomerDEANR], [DE_CustomerNPINR],
[DE_CustomerHINNR], [DE_CustomerName], [DE_CustomerAddress1],
[DE_CustomerAddress2], [DE_CustomerCity], [DE_CustomerState],
[DE_CustomerZipcode], [DE_CustomerCountry], [DE_InvoiceNR], [DE_Invoicedate],
[DE_QuantitySold], [DE_QuantityReturned], [DE_SalesReturnCode],
[DE_ContractNR], [DE_Price])
VALUES(@OriginalFilename, @InterchangeReceiverID, @Distributor,
@DE_Date, @DE_VendorNR, @DE_VendorDEANR, @DE_VendorName,
@DE_DistributionCenterNR, @DE_DistributionCenterID,
@DE_DistributionCenterName, @DE_ItemNR, @DE_ItemNDCNR, @DE_ProductUOM,
@DE_CustomerAccountNR, @DE_CustomerDEANR, @DE_CustomerNPINR,
@DE_CustomerHINNR, @DE_CustomerName, @DE_CustomerAddress1,
@DE_CustomerAddress2, @DE_CustomerCity, @DE_CustomerState,
@DE_CustomerZipcode, @DE_CustomerCountry, @DE_InvoiceNR, @DE_Invoicedate,
@DE_QuantitySold, @DE_QuantityReturned, @DE_SalesReturnCode,
@DE_ContractNR, @DE_Price)
我像这样遍历所有参数:
command.Parameters.Add(new SqlParameter("@InterchangeReceiverID", SqlDbType.NVarChar,500));
command.Parameters["@InterchangeReceiverID"].Value = "VALUEGOESHERE";
我也尝试了该Parameters.Add(name,value)
方法,但已弃用。
我不断收到的错误是:
System.InvalidOperationException:SqlCommand.Prepare 方法要求所有参数都具有明确设置的类型。
不幸的是,他没有提到导致问题的参数。我正在使用大小为 500 的 NVarchar 和大小为 18 且比例为 6 的小数。
我做错了什么,或者我怎样才能找出导致问题的参数?我将所有参数写入文件(带有大小/比例和值),所有参数看起来都很好......
完整代码:
SqlCommand command = GetNewSQLCommand(SALESDATAQRY);
//command.CommandText = SALESDATAQRY;
logger.Info(command.CommandText);
command.Parameters.Add(new SqlParameter("@InterchangeReceiverID", SqlDbType.NVarChar,500));
command.Parameters["@InterchangeReceiverID"].Value = obj.INTERCHANGE.INTERCHANGE_HEADER.INTERCHANGE_RECEIVER_ID;
command.Parameters.Add(new SqlParameter("@DE_VendorNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_VendorNR"].Value= obj.INTERCHANGE.INTERCHANGE_HEADER.INTERCHANGE_RECEIVER_ID;
foreach (PRODUCT_TRANSFER_AND_RESALE_REPORTINTERCHANGEFUNCTIONAL_GROUP funcGroup in obj.INTERCHANGE.FUNCTIONAL_GROUP)
{
if (command.Parameters.Contains("@OriginalFilename")) command.Parameters.Remove("@OriginalFilename");
command.Parameters.Add(new SqlParameter("@OriginalFilename", SqlDbType.NVarChar,500)) ;
command.Parameters["@OriginalFilename"].Value = filename;
command.Parameters.Add(new SqlParameter("@Distributor", SqlDbType.NVarChar,500)) ;
command.Parameters["@Distributor"].Value = "-";
foreach (PRODUCT_TRANSFER_AND_RESALE_REPORTINTERCHANGEFUNCTIONAL_GROUPTRANSACTIONNAME_INFO ni in funcGroup.TRANSACTION.NAME_INFO){
if (command.Parameters.Contains("@DE_DistributionCenterNR")) command.Parameters.Remove("@DE_DistributionCenterNR");
if (command.Parameters.Contains("@DE_DistributionCenterID")) command.Parameters.Remove("@DE_DistributionCenterID");
if (command.Parameters.Contains("@DE_DistributionCenterName")) command.Parameters.Remove("@DE_DistributionCenterName");
if (ni.IDENTIFICATION_CODE_QUALIFIER.Equals("ZZ"))
{
if (command.Parameters.Contains("@Distributor")) command.Parameters.Remove("@Distributor");
command.Parameters.Add(new SqlParameter("@Distributor", SqlDbType.NVarChar,500)) ;
command.Parameters["@Distributor"].Value = ni.IDENTIFICATION_CODE;
}
command.Parameters.Add(new SqlParameter("@DE_DistributionCenterNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_DistributionCenterNR"].Value = ni.IDENTIFICATION_CODE;
command.Parameters.Add(new SqlParameter("@DE_DistributionCenterID", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_DistributionCenterID"].Value = ni.IDENTIFICATION_CODE;//identifier code intern
command.Parameters.Add(new SqlParameter("@DE_DistributionCenterName", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_DistributionCenterName"].Value = ni.NAME;//iodentifeirname
}
foreach (DTM dtm in funcGroup.TRANSACTION.DTM){
if (dtm.DATE_QUALIFIER.Equals("090")) {
if (command.Parameters.Contains("@DE_Date")) command.Parameters.Remove("@DE_Date");
command.Parameters.Add(new SqlParameter("@DE_Date", SqlDbType.DateTime )) ;
command.Parameters["@DE_Date"].Value=dtm.DATE; //090 start, 091 end
}
}
if (command.Parameters.Contains("@DE_VendorDEANR")) command.Parameters.Remove("@DE_VendorDEANR");
if (command.Parameters.Contains("@DE_VendorName")) command.Parameters.Remove("@DE_VendorName");
command.Parameters.Add(new SqlParameter("@DE_VendorDEANR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_VendorDEANR"].Value = "-";
command.Parameters.Add(new SqlParameter("@DE_VendorName", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_VendorName"].Value = "-";
foreach (PRODUCT_TRANSFER_AND_RESALE_REPORTINTERCHANGEFUNCTIONAL_GROUPTRANSACTIONPTD ptd in funcGroup.TRANSACTION.PTD){
if (command.Parameters.Contains("@DE_ItemNR")) command.Parameters.Remove("@DE_ItemNR");
if (command.Parameters.Contains("@DE_ItemNDCNR")) command.Parameters.Remove("@DE_ItemNDCNR");
if (command.Parameters.Contains("@DE_ProductUOM")) command.Parameters.Remove("@DE_ProductUOM");
if (command.Parameters.Contains("@DE_CustomerAccountNR")) command.Parameters.Remove("@DE_CustomerAccountNR");
if (command.Parameters.Contains("@DE_CustomerDEANR")) command.Parameters.Remove("@DE_CustomerDEANR");
if (command.Parameters.Contains("@DE_CustomerNPINR")) command.Parameters.Remove("@DE_CustomerNPINR");
if (command.Parameters.Contains("@DE_CustomerHINNR")) command.Parameters.Remove("@DE_CustomerHINNR");
if (command.Parameters.Contains("@DE_CustomerName")) command.Parameters.Remove("@DE_CustomerName");
if (command.Parameters.Contains("@DE_CustomerCity")) command.Parameters.Remove("@DE_CustomerCity");
if (command.Parameters.Contains("@DE_CustomerState")) command.Parameters.Remove("@DE_CustomerState");
if (command.Parameters.Contains("@DE_CustomerZipcode")) command.Parameters.Remove("@DE_CustomerZipcode");
if (command.Parameters.Contains("@DE_CustomerCountry")) command.Parameters.Remove("@DE_CustomerCountry");
if (command.Parameters.Contains("@DE_QuantitySold")) command.Parameters.Remove("@DE_QuantitySold");
if (command.Parameters.Contains("@DE_QuantityReturned")) command.Parameters.Remove("@DE_QuantityReturned");
if (command.Parameters.Contains("@DE_SalesReturnCode")) command.Parameters.Remove("@DE_SalesReturnCode");
if (command.Parameters.Contains("@DE_ContractNR")) command.Parameters.Remove("@DE_ContractNR");
if (command.Parameters.Contains("@DE_Price")) command.Parameters.Remove("@DE_Price");
command.Parameters.Add(new SqlParameter("@DE_ItemNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_ItemNR"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.PRODUCT_SERVICE_ID;
command.Parameters.Add(new SqlParameter("@DE_ItemNDCNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_ItemNDCNR"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.PRODUCT_SERVICE_ID;
command.Parameters.Add(new SqlParameter("@DE_ProductUOM", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_ProductUOM"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.UNIT_OR_BASIS_FOR_MEASUREMENT_CODE;
command.Parameters.Add(new SqlParameter("@DE_CustomerAccountNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerAccountNR"].Value = ptd.NAME_INFO.IDENTIFICATION_CODE;
if (ptd.NAME_INFO.IDENTIFICATION_CODE_QUALIFIER.Equals("11")){
command.Parameters.Add(new SqlParameter("@DE_CustomerDEANR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerDEANR"].Value = ptd.NAME_INFO.IDENTIFICATION_CODE;
command.Parameters.Add(new SqlParameter("@DE_CustomerNPINR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerNPINR"].Value = "-";
command.Parameters.Add(new SqlParameter("@DE_CustomerHINNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerHINNR"].Value = "-";
}else if (ptd.NAME_INFO.IDENTIFICATION_CODE_QUALIFIER.Equals("21")){
command.Parameters.Add(new SqlParameter("@DE_CustomerDEANR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerDEANR"].Value = "-";
command.Parameters.Add(new SqlParameter("@DE_CustomerNPINR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerNPINR"].Value = "-";
command.Parameters.Add(new SqlParameter("@DE_CustomerHINNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerHINNR"].Value = ptd.NAME_INFO.IDENTIFICATION_CODE;
}
command.Parameters.AddWithValue("@DE_CustomerName", SqlDbType.NVarChar ).Value=ptd.NAME_INFO.NAME;
foreach (PRODUCT_TRANSFER_AND_RESALE_REPORTINTERCHANGEFUNCTIONAL_GROUPTRANSACTIONPTDNAME_INFOADDRESS_INFO add in ptd.NAME_INFO.ADDRESS_INFO){
if (command.Parameters.Contains("@DE_CustomerAddress1")) command.Parameters.Remove("@DE_CustomerAddress1");
if (command.Parameters.Contains("@DE_CustomerAddress2")) command.Parameters.Remove("@DE_CustomerAddress2");
command.Parameters.Add(new SqlParameter("@DE_CustomerAddress1", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerAddress1"].Value = add.ADDRESS_INFORMATION_1;
command.Parameters.Add(new SqlParameter("@DE_CustomerAddress2", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerAddress2"].Value = add.ADDRESS_INFORMATION_2;
}
command.Parameters.Add(new SqlParameter("@DE_CustomerCity", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerCity"].Value = ptd.NAME_INFO.GEOGRAPHIC_LOCATION.CITY_NAME;
command.Parameters.Add(new SqlParameter("@DE_CustomerState", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerState"].Value = ptd.NAME_INFO.GEOGRAPHIC_LOCATION.STATE_OR_PROVINCE_CODE;
command.Parameters.Add(new SqlParameter("@DE_CustomerZipcode", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerZipcode"].Value = ptd.NAME_INFO.GEOGRAPHIC_LOCATION.POSTAL_CODE;
command.Parameters.Add(new SqlParameter("@DE_CustomerCountry", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_CustomerCountry"].Value = ptd.NAME_INFO.GEOGRAPHIC_LOCATION.COUNTRY_CODE;
foreach (PRODUCT_TRANSFER_AND_RESALE_REPORTINTERCHANGEFUNCTIONAL_GROUPTRANSACTIONPTDNAME_INFOSALES_ITEM_INFORMATIONREF_ID refid in ptd.NAME_INFO.SALES_ITEM_INFORMATION.REF_ID ){
if (refid.REFERENCE_IDENTIFICATION_QUALIFIER.Contains("DI")){
if (command.Parameters.Contains("@DE_InvoiceNR")) command.Parameters.Remove("@DE_InvoiceNR");
if (command.Parameters.Contains("@DE_Invoicedate")) command.Parameters.Remove("@DE_Invoicedate");
command.Parameters.Add(new SqlParameter("@DE_InvoiceNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_InvoiceNR"].Value = refid.REFERENCE_IDENTIFICATION;
command.Parameters.Add(new SqlParameter("@DE_Invoicedate", SqlDbType.DateTime)) ;
command.Parameters["@DE_Invoicedate"].Value = refid.DATE;
}
}
command.Parameters.Add(new SqlParameter("@DE_QuantitySold", SqlDbType.Decimal, 18)) ;
command.Parameters["@DE_QuantitySold"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.QUANTITY;
command.Parameters.Add(new SqlParameter("@DE_QuantityReturned", SqlDbType.Decimal,18)) ;
command.Parameters["@DE_QuantityReturned"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.QUANTITY;
command.Parameters.Add(new SqlParameter("@DE_SalesReturnCode", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_SalesReturnCode"].Value = "-";
command.Parameters.Add(new SqlParameter("@DE_ContractNR", SqlDbType.NVarChar,500)) ;
command.Parameters["@DE_ContractNR"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.PRODUCT_SERVICE_ID;
command.Parameters.Add(new SqlParameter("@DE_Price", SqlDbType.Decimal,18)) ;
command.Parameters["@DE_Price"].Value = ptd.NAME_INFO.SALES_ITEM_INFORMATION.MONETARY_AMOUNT;
for (int i = 0; i < command.Parameters.Count; i++)
{
if (command.Parameters[i].SqlDbType == SqlDbType.NVarChar) command.Parameters[i].Size = 500;
if (command.Parameters[i].SqlDbType == SqlDbType.Decimal)
{
command.Parameters[i].Size = 18;
command.Parameters[i].Scale = 6;
}
if (!SALESDATAQRY.Contains(command.Parameters[i].ParameterName)) logger.Error("Parameter not found: " + command.Parameters[i].ParameterName);
logger.Info(command.Parameters[i].ParameterName + ": " + command.Parameters[i].SqlDbType + " " + command.Parameters[i].Size + " " + command.Parameters[i].Scale + " " + command.Parameters[i].Value);
}
command.Prepare();
logger.Info("inserted " + command.ExecuteNonQuery()+ " rows");
}
}