我试图从源中获取数据并保存到数据库中,但是即使声明了标量变量,我仍然面临着这个问题。在下面找到我尝试的方法。我面临的问题:System.Data.SqlClient.SqlException:'必须声明标量变量“ @col_shipping_price”。'
class Program
{
static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(AmazonEnvelope));
TextReader reader = new StreamReader(@"C:\Users\*********\Desktop\16315550943018039.xml");
object obj = deserializer.Deserialize(reader);
AmazonEnvelope XmlData = (AmazonEnvelope)obj;
reader.Close();
SqlConnection cnn = new SqlConnection(@"Data Source=ABDUL-TPS\TPSSQLSERVER;Initial Catalog=Zoho_Amz_API;User ID=zohoapiservice;Password=**********");
cnn.Open();
for (int i = 0; i < XmlData.Message.Count; i++)
{
string sqlquery = "if not exists (select * from tbl_AMZ_API_sample where col_sku like '" + XmlData.Message[i].Order.OrderItem.SKU + "') insert into tbl_AMZ_API_sample(col_amazon_order_id, col_merchant_order_id, col_purchase_date, col_last_updated_date, col_order_status, col_fulfillment_channel, col_sales_channel, col_order_channel, col_url, col_ship_service_level, col_product_name, col_sku, col_asin, col_number_of_items, col_item_status, col_quantity, col_currency, col_item_price, col_item_tax, col_shipping_price, col_shipping_tax, col_gift_wrap_price, col_gift_wrap_tax, col_item_promotion_discount, col_ship_promotion_discount, col_ship_city, col_ship_state, col_ship_postal_code, col_ship_country, col_promotion_ids, col_is_business_order, col_purchase_order_number, col_price_designation, col_fulfilled_by, col_last_update_time) values(@col_amazon_order_id, @col_merchant_order_id, @col_purchase_date, @col_last_updated_date, @col_order_status, @col_fulfillment_channel, @col_sales_channel, @col_order_channel, @col_url, @col_ship_service_level, @col_product_name, @col_sku, @col_asin, @col_number_of_items, @col_item_status, @col_quantity, @col_currency, @col_item_price, @col_item_tax, @col_shipping_price, @col_shipping_tax, @col_gift_wrap_price, @col_gift_wrap_tax, @col_item_promotion_discount, @col_ship_promotion_discount, @col_ship_city, @col_ship_state, @col_ship_postal_code, @col_ship_country, @col_promotion_ids, @col_is_business_order, @col_purchase_order_number, @col_price_designation, @col_fulfilled_by, @col_last_update_time)";
SqlCommand cmd = new SqlCommand(sqlquery, cnn);
for (int j = 0; j < XmlData.Message[i].Order.OrderItem.ItemPrice.Component.Count; j++)
{
cmd.Parameters.AddWithValue("@col_amazon_order_id", XmlData.Message[i].Order.AmazonOrderID);
if (XmlData.Message[i].Order.MerchantOrderID == null)
{
cmd.Parameters.AddWithValue("@col_merchant_order_id", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@col_merchant_order_id", XmlData.Message[i].Order.MerchantOrderID);
}
cmd.Parameters.AddWithValue("@col_purchase_date", XmlData.Message[i].Order.PurchaseDate);
cmd.Parameters.AddWithValue("@col_last_updated_date", Global.unique.ToString());
cmd.Parameters.AddWithValue("@col_order_status", XmlData.Message[i].Order.OrderStatus);
cmd.Parameters.AddWithValue("@col_fulfillment_channel", XmlData.Message[i].Order.FulfillmentData.FulfillmentChannel);
cmd.Parameters.AddWithValue("@col_sales_channel", XmlData.Message[i].Order.SalesChannel);
cmd.Parameters.AddWithValue("@col_ship_service_level", XmlData.Message[i].Order.FulfillmentData.ShipServiceLevel);
cmd.Parameters.AddWithValue("@col_product_name", XmlData.Message[i].Order.OrderItem.ProductName);
cmd.Parameters.AddWithValue("@col_sku", XmlData.Message[i].Order.OrderItem.SKU);
cmd.Parameters.AddWithValue("@col_asin", XmlData.Message[i].Order.OrderItem.ASIN);
cmd.Parameters.AddWithValue("@col_number_of_items", XmlData.Message[i].Order.OrderItem.NumberOfItems);
cmd.Parameters.AddWithValue("@col_item_status", XmlData.Message[i].Order.OrderItem.ItemStatus);
cmd.Parameters.AddWithValue("@col_quantity", XmlData.Message[i].Order.OrderItem.Quantity);
cmd.Parameters.AddWithValue("@col_currency", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Currency);
switch (XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Type)
{
case "Principal":
cmd.Parameters.AddWithValue("@col_item_price", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
cmd.Parameters.AddWithValue("@col_item_tax", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
break;
case "Shipping":
cmd.Parameters.AddWithValue("@col_shipping_price", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
break;
case "GiftWrap":
cmd.Parameters.AddWithValue("@col_gift_wrap_price", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
break;
case "Shipping-Tax":
cmd.Parameters.AddWithValue("@col_shipping_tax", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
break;
default:
cmd.Parameters.AddWithValue("@col_gift_wrap_tax", XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Amount.Text);
break;
}
if (XmlData.Message[i].Order.OrderItem.Promotion == null)
{
cmd.Parameters.AddWithValue("@col_item_promotion_discount", 0);
cmd.Parameters.AddWithValue("@col_ship_promotion_discount", 0);
cmd.Parameters.AddWithValue("@col_promotion_ids", 0);
}
else
{
cmd.Parameters.AddWithValue("@col_item_promotion_discount", XmlData.Message[i].Order.OrderItem.Promotion.ItemPromotionDiscount);
cmd.Parameters.AddWithValue("@col_ship_promotion_discount", XmlData.Message[i].Order.OrderItem.Promotion.ShipPromotionDiscount);
cmd.Parameters.AddWithValue("@col_promotion_ids", XmlData.Message[i].Order.OrderItem.Promotion.PromotionIDs);
}
cmd.Parameters.AddWithValue("@col_ship_city", XmlData.Message[i].Order.FulfillmentData.Address.City);
cmd.Parameters.AddWithValue("@col_ship_state", XmlData.Message[i].Order.FulfillmentData.Address.State);
cmd.Parameters.AddWithValue("@col_ship_postal_code", XmlData.Message[i].Order.FulfillmentData.Address.PostalCode);
cmd.Parameters.AddWithValue("@col_ship_country", XmlData.Message[i].Order.FulfillmentData.Address.Country);
cmd.Parameters.AddWithValue("@col_is_business_order", XmlData.Message[i].Order.IsBusinessOrder);
cmd.Parameters.AddWithValue("@col_purchase_order_number", XmlData.Message[i].Order.PurchaseOrderNumber);
cmd.Parameters.AddWithValue("@col_price_designation", XmlData.Message[i].Order.OrderItem.PriceDesignation);
cmd.Parameters.AddWithValue("@col_fulfilled_by", XmlData.Message[i].Order.FulfilledBy);
cmd.Parameters.AddWithValue("@col_Order_Channel", DBNull.Value);
cmd.Parameters.AddWithValue("@col_url", DBNull.Value);
Console.WriteLine(XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Type);
cmd.ExecuteNonQuery();
}
}
Console.ReadKey();
}
}
请帮助。
答案 0 :(得分:2)
您显然通常会知道如何使用参数化查询,所以为什么不在查询的SELECT
部分中使用它:
... like '" + XmlData.Message[i].Order.OrderItem.SKU + "' ...
您还应该在此处使用参数。除非LIKE
包含通配符,否则=
可以更改为XmlData.Message[i].Order.OrderItem.SKU
,这似乎不太可能。
关于您的问题:
查询的@col_shipping_price
部分中有INSERT
,显然是作为参数的。但是,只有在XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Type
中Shipping
是switch()
的情况下,才能设置此参数。也就是说,在所有其他情况下,@col_shipping_price
保留在查询中,并且不会被替换。 SQL Server认为它是一个变量,因此理所当然地抱怨它没有被声明。似乎其他一些参数也可能出现相同的问题。
您在这里有一些选择。
DBNull
。除非应该起作用的列上没有空约束。如果您这样做的话,也不需要在每次迭代中都重建查询。创建一次,可以选择Prepare()
,然后在迭代中更改参数的值。性能可能会因此受益。XmlData.Message[i].Order.OrderItem.ItemPrice.Component[j].Type
建立不同的查询。这样,您可以简单地将未使用参数的占位符排除在外。使用AddWithValue()
时要小心,甚至要避免完全使用它。它需要猜测数据库中列的数据类型,有时很不幸地失败并产生有趣的错误。最好将Add()
重载与显式类型参数一起使用。关于此主题的一些内容:"AddWithValue is Evil"