如何使用c#从xml向数据库添加数据

时间:2018-03-09 13:12:56

标签: c# sql

这是我的代码。请帮我。我无法将记录添加到数据库中。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--OrderFlow will have varchar Type M-Mail in Art, B- Bring Art To Store, N- Normal Orders-->
<ns:orders xmlns:ns="http://www.demandware.com/xml/impex/order/2006-10-31" Version="17.2.1.0" StoreNumber="9291" EventName="OrderCreation_Ecom_to_FP" Source="Ecom" IsReady="0" OrderFlow="M">
    <ns:order xmlns="http://www.demandware.com/xml/impex/order/2006-10-31" OrderNumber="00008310W01" Encoding="ISO-8859-1" CartOrderNumber="00008310" PhysicalStoreNumber="7383" OriginalOrderNumber="00008210W01">
        <ns:CreateDate>2018-03-06T08:08:15</ns:CreateDate>
        <ns:DueDate>2018-03-06T08:08:15</ns:DueDate>
        <ns:Quantity>1</ns:Quantity>
        <ns:RetailSubTotal>159</ns:RetailSubTotal>
        <ns:DiscountedSubTotal>143.1</ns:DiscountedSubTotal>
        <ns:Tax>9.3</ns:Tax>
        <ns:TotalPrice>152.4</ns:TotalPrice>
        <ns:DiscountAmount>-15.9</ns:DiscountAmount>
        <ns:TotalPriceLessTax>143.1</ns:TotalPriceLessTax>
        <ns:TaxableSubtotal>143.1</ns:TaxableSubtotal>
        <ns:NonTaxableSubtotal>0.0</ns:NonTaxableSubtotal>
        <ns:UseStretcherBar>0</ns:UseStretcherBar>
        <ns:Notes>Framed Photo Print</ns:Notes>
        <ns:OrderType>Custom</ns:OrderType>
        <ns:OrderStatus>Audited</ns:OrderStatus>
        <ns:Framewidth>38.00</ns:Framewidth>
        <ns:FrameHeight>29.50</ns:FrameHeight>
        <ns:TotalOutsideWidth>39.54</ns:TotalOutsideWidth>
        <ns:TotalOutSideHeight>31.04</ns:TotalOutSideHeight>
        <ns:previewImageURL>https://previews.framerspointe.com/image/upload/bo_0px_solid_rgb:ffffff,c_crop,h_2412,w_3228,x_18,y_18/c_scale,h_2448,w_3264/h_2796,u_Mats:B8260,w_3612/fl_layer_apply/h_99,l_Frames:2951261TL,w_3810/fl_layer_apply,g_north,y_-99/a_180,h_99,l_Frames:2951261BR,w_3810/fl_layer_apply,g_south,y_-99/h_99,l_Frames:2951261BR,w_2994/c_lpad,g_north,h_3810,w_2994/a_90/l_triangle,w_99/a_180/e_cut_out,fl_layer_apply,g_north_east/l_triangle,w_99/a_90/e_cut_out,fl_layer_apply,g_south_east/fl_layer_apply/h_99,l_Frames:2951261TL,w_2994/c_lpad,g_north,h_3810,w_2994/a_-90/l_triangle,w_99/a_-90/e_cut_out,fl_layer_apply,g_north_west/l_triangle,w_99/e_cut_out,fl_layer_apply,g_south_west/fl_layer_apply/c_scale,w_400/pqqiethajvrz3lstvdqs</ns:previewImageURL>
        <ns:ExpediteShippingPrice IsExpedite="0" CartQty="1">
            <ns:RetailPrice>0</ns:RetailPrice>
            <ns:SalePrice>0</ns:SalePrice>
            <ns:Tax>0</ns:Tax>
            <ns:DiscountedAmount>0</ns:DiscountedAmount>
        </ns:ExpediteShippingPrice>
        <ns:MailInArt-address>
            <ns:customer-no>29832996</ns:customer-no>
            <ns:customer-email>Amberry1208@gmail.com</ns:customer-email>
            <ns:FirstName>Angela</ns:FirstName>
            <ns:LastName>Berry</ns:LastName>
            <ns:StreetA>7111 W 56TH ST</ns:StreetA>
            <ns:StreetB>#40</ns:StreetB>
            <ns:City>SIOUX FALLS</ns:City>
            <ns:PostalCode>57106-7593</ns:PostalCode>
            <ns:State_id>SD</ns:State_id>
            <ns:country-code>us</ns:country-code>
            <ns:phone>605-254-5931</ns:phone>
            <ns:ShippingType>001</ns:ShippingType>
        </ns:MailInArt-address>
        <ns:shipping-address>
            <ns:customer-no>29832996</ns:customer-no>
            <ns:customer-email>Amberry1208@gmail.com</ns:customer-email>
            <ns:FirstName>Angela</ns:FirstName>
            <ns:LastName>Berry</ns:LastName>
            <ns:StreetA>8607 Allisonville Rd</ns:StreetA>
            <ns:StreetB>Store #7383</ns:StreetB>
            <ns:City>Indianapolis</ns:City>
            <ns:PostalCode>46250</ns:PostalCode>
            <ns:State_id>IN</ns:State_id>
            <ns:country-code>us</ns:country-code>
            <ns:phone>605-254-5931</ns:phone>
            <ns:ShippingType>001</ns:ShippingType>
        </ns:shipping-address>
        <ns:Backing>
            <ns:Sku>SFC</ns:Sku>
            <ns:Layer />
            <ns:RetailPrice>0</ns:RetailPrice>
            <ns:SalePrice>0</ns:SalePrice>
            <ns:DiscountedAmount>0</ns:DiscountedAmount>
            <ns:Width>38.00</ns:Width>
            <ns:Height>29.50</ns:Height>
        </ns:Backing>
        <ns:Fitting>
            <ns:Sku>BFIT</ns:Sku>
            <ns:Layer />
            <ns:RetailPrice>0</ns:RetailPrice>
            <ns:SalePrice>0</ns:SalePrice>
            <ns:DiscountedAmount>0</ns:DiscountedAmount>
            <ns:Width>38.00</ns:Width>
            <ns:Height>29.50</ns:Height>
        </ns:Fitting>
        <ns:Glass>
            <ns:Sku>AC</ns:Sku>
            <ns:Layer />
            <ns:RetailPrice>0</ns:RetailPrice>
            <ns:SalePrice>0</ns:SalePrice>
            <ns:DiscountedAmount>0</ns:DiscountedAmount>
            <ns:Width>38.00</ns:Width>
            <ns:Height>29.50</ns:Height>
        </ns:Glass>
        <ns:Images>
            <ns:Image ShapeType="Rectangle" DesignNumber="101">
                <ns:DPI>96</ns:DPI>
                <ns:Width>34</ns:Width>
                <ns:Height>25.5</ns:Height>
                <ns:RetailPrice>39</ns:RetailPrice>
                <ns:SalePrice>35.1</ns:SalePrice>
                <ns:DiscountedAmount>3.9</ns:DiscountedAmount>
                <ns:LeftOffset>1.813</ns:LeftOffset>
                <ns:TopOffset>1.813</ns:TopOffset>
                <ns:RightOffset>35.81</ns:RightOffset>
                <ns:BottomOffset>27.31</ns:BottomOffset>
                <ns:RightBorder>1.8125</ns:RightBorder>
                <ns:BottomBorder>1.8125</ns:BottomBorder>
                <ns:LeftBorder>1.8125</ns:LeftBorder>
                <ns:TopBorder>1.8125</ns:TopBorder>
                <ns:sourceImageURL>https://previews.framerspointe.com/image/upload/pqqiethajvrz3lstvdqs</ns:sourceImageURL>
                <ns:croppedImageURL>https://previews.framerspointe.com/image/upload/q_100/c_crop,dn_96,h_2448,w_3264,x_0,y_0/pqqiethajvrz3lstvdqs</ns:croppedImageURL>
                <ns:Description>Framed Photo Print</ns:Description>
                <ns:LibraryImageUsed>0</ns:LibraryImageUsed>
                <ns:SubstrateType>Paper</ns:SubstrateType>
                <ns:Mount>
                    <ns:Sku>Dry Mount</ns:Sku>
                    <ns:RetailPrice>0</ns:RetailPrice>
                    <ns:SalePrice>0</ns:SalePrice>
                    <ns:DiscountedAmount>0</ns:DiscountedAmount>
                    <ns:Width>34</ns:Width>
                    <ns:Height>25.5</ns:Height>
                </ns:Mount>
            </ns:Image>
        </ns:Images>
        <ns:Frames>
            <ns:Frame>
                <ns:Sku>2951261</ns:Sku>
                <ns:Layer />
                <ns:RetailPrice>75</ns:RetailPrice>
                <ns:SalePrice>67.5</ns:SalePrice>
                <ns:DiscountedAmount>7.5</ns:DiscountedAmount>
                <ns:Width>38.00</ns:Width>
                <ns:Height>29.50</ns:Height>
            </ns:Frame>
        </ns:Frames>
        <ns:Mats>
            <ns:Mat>
                <ns:Sku>B8260</ns:Sku>
                <ns:Layer>1</ns:Layer>
                <ns:RetailPrice>45</ns:RetailPrice>
                <ns:SalePrice>40.5</ns:SalePrice>
                <ns:DiscountedAmount>4.5</ns:DiscountedAmount>
                <ns:Width>38.00</ns:Width>
                <ns:Height>29.50</ns:Height>
                <ns:RightBorder>2.188</ns:RightBorder>
                <ns:BottomBorder>2.188</ns:BottomBorder>
                <ns:LeftBorder>2.188</ns:LeftBorder>
                <ns:TopBorder>2.188</ns:TopBorder>
                <ns:Opening Height="25.13" Width="33.63" />
            </ns:Mat>
        </ns:Mats>
    </ns:order>
</ns:orders>

C#:

DateTime? date=null;
DateTime? DueDate = null;
string OriginalOrderNumber = null;
string FirstName=null;
string LastName = null;
string email = null;
string StreetA = null;
string StreetB = null;
string PostalCode = null;
string City = null;
string State_id = null;
string country_code = null;
string phone = null;

XmlTextReader reader = new XmlTextReader("ecom_MailInArt_Store.xml");

while (reader.Read())
{

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:order"))
    {
        if (reader.HasAttributes)
        {
            OriginalOrderNumber = reader.GetAttribute("OriginalOrderNumber");

        }

    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:CreateDate"))
    {
        reader.Read();
        date = Convert.ToDateTime(reader.Value);

    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:DueDate"))
    {
        reader.Read();
        DueDate = Convert.ToDateTime(reader.Value);
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:FirstName"))
    {
        reader.Read();
        FirstName = reader.Value;

    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:LastName"))
    {
        reader.Read();
        LastName = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:customer-email"))
    {
        reader.Read();
        email = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:phone"))
    {
        reader.Read();
        phone = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:StreetA"))
    {
        reader.Read();
        StreetA = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:StreetB"))
    {
        reader.Read();
        StreetB = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:PostalCode"))
    {
        reader.Read();
        PostalCode = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:City"))
    {
        reader.Read();
        City = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:State_id"))
    {
        reader.Read();
        State_id = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:country-code"))
    {
        reader.Read();
        country_code = reader.Value;
    }

    SqlConnection con = new SqlConnection("Data Source=MNGNET320543D;Initial Catalog=excercise1;User id=sa;Password=Infosys123;");
    SqlCommand cmd = new SqlCommand("Insert into MailInArtOrder(OrderNumber,OrderCreateDate,DueDate,FirstName,LastName,EmailAddress,PhoneNumber,StreetA,StreetB,PostalCode,City,State,CountryCode) values(@OrderNumber,@OrderCreateDate,@DueDate,@FirstName,@LastName,@EmailAddress,@PhoneNumber,@StreetA,@StreetB,@PostalCode,@City,@State,@CountryCode)", con);

    cmd.Parameters.AddWithValue("@OrderNumber", OriginalOrderNumber);
    cmd.Parameters["@OrderNumber"].Value = OriginalOrderNumber;
    cmd.Parameters.AddWithValue("@OrderCreateDate", date);
    cmd.Parameters.AddWithValue("@DueDate", DueDate);
    cmd.Parameters.AddWithValue("@FirstName", FirstName);
    cmd.Parameters.AddWithValue("@LastName", LastName);
    cmd.Parameters.AddWithValue("@EmailAddress", email);
    cmd.Parameters.AddWithValue("@PhoneNumber", phone);
    cmd.Parameters.AddWithValue("@StreetA", StreetA);
    cmd.Parameters.AddWithValue("@StreetB", StreetB);
    cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
    cmd.Parameters.AddWithValue("@City", City);
    cmd.Parameters.AddWithValue("@State", State_id);
    cmd.Parameters.AddWithValue("@CountryCode", country_code);

    try
    {
        con.Open();
        SqlDataReader reader1 = cmd.ExecuteReader();
        Console.WriteLine("Record inserted");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

请帮帮我。我希望将这些记录添加到我的数据库中。

我收到此错误:

  

参数化查询'(@ OrderNumber nvarchar(11),@ OrderCreateDate datetime,@ DueDate da'需要参数'@PhoneNumber',未提供

如果我尝试在while循环后插入数据。它会插入一条记录。

我想要所有记录。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

在你的时间里,对于每个循环,你只能获得一个信息。但是您尝试在每个循环的数据库中插入。 您必须获取所有信息,然后在数据库中插入所有信息。

试试这个:

DateTime? date=null;
DateTime? DueDate = null;
string OriginalOrderNumber = null;
string FirstName=null;
string LastName = null;
string email = null;
string StreetA = null;
string StreetB = null;
string PostalCode = null;
string City = null;
string State_id = null;
string country_code = null;
string phone = null;

XmlTextReader reader = new XmlTextReader("ecom_MailInArt_Store.xml");

while (reader.Read())
{

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:order"))
    {
        if (reader.HasAttributes)
        {
            OriginalOrderNumber = reader.GetAttribute("OriginalOrderNumber");

        }

    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:CreateDate"))
    {
        reader.Read();
        date = Convert.ToDateTime(reader.Value);

    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:DueDate"))
    {
        reader.Read();
        DueDate = Convert.ToDateTime(reader.Value);
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:FirstName"))
    {
        reader.Read();
        FirstName = reader.Value;

    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:LastName"))
    {
        reader.Read();
        LastName = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:customer-email"))
    {
        reader.Read();
        email = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:phone"))
    {
        reader.Read();
        phone = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:StreetA"))
    {
        reader.Read();
        StreetA = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:StreetB"))
    {
        reader.Read();
        StreetB = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:PostalCode"))
    {
        reader.Read();
        PostalCode = reader.Value;
    }

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:City"))
    {
        reader.Read();
        City = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:State_id"))
    {
        reader.Read();
        State_id = reader.Value;
    }
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "ns:country-code"))
    {
        reader.Read();
        country_code = reader.Value;
    }
}
SqlConnection con = new SqlConnection("Data Source=MNGNET320543D;Initial Catalog=excercise1;User id=sa;Password=Infosys123;");
SqlCommand cmd = new SqlCommand("Insert into MailInArtOrder(OrderNumber,OrderCreateDate,DueDate,FirstName,LastName,EmailAddress,PhoneNumber,StreetA,StreetB,PostalCode,City,State,CountryCode) values(@OrderNumber,@OrderCreateDate,@DueDate,@FirstName,@LastName,@EmailAddress,@PhoneNumber,@StreetA,@StreetB,@PostalCode,@City,@State,@CountryCode)", con);

cmd.Parameters.AddWithValue("@OrderNumber", OriginalOrderNumber);
cmd.Parameters["@OrderNumber"].Value = OriginalOrderNumber;
cmd.Parameters.AddWithValue("@OrderCreateDate", date);
cmd.Parameters.AddWithValue("@DueDate", DueDate);
cmd.Parameters.AddWithValue("@FirstName", FirstName);
cmd.Parameters.AddWithValue("@LastName", LastName);
cmd.Parameters.AddWithValue("@EmailAddress", email);
cmd.Parameters.AddWithValue("@PhoneNumber", phone);
cmd.Parameters.AddWithValue("@StreetA", StreetA);
cmd.Parameters.AddWithValue("@StreetB", StreetB);
cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
cmd.Parameters.AddWithValue("@City", City);
cmd.Parameters.AddWithValue("@State", State_id);
cmd.Parameters.AddWithValue("@CountryCode", country_code);

try
{
    con.Open();
    SqlDataReader reader1 = cmd.ExecuteReader();
    Console.WriteLine("Record inserted");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

希望有所帮助。