Я пытаюсь получить данные из источника и сохранить их в базе данных, но я сталкиваюсь с этой проблемой даже после объявления скалярной переменной. найдите ниже подход, который я попробовал. Проблема, с которой я сталкиваюсь: 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();
}
}
Кто-нибудь, помогите, пожалуйста.