sqlbulkcopy错误插入2个表

时间:2015-05-25 05:02:00

标签: c# sql-server-2008 sqlbulkcopy

您好我试图将xml文件的详细信息保存到两个表中。插件能够在第一个表上成功运行,但当它到达第二个表时,我收到一条错误消息" ArgumentNullException未处理:值不能为空。"。我甚至确保我的表允许null但我仍然得到错误。以下是我的代码。

 private void btnStore_Click(object sender, EventArgs e)
    {
        try
        {
            string cs = @"Data Source=localhost;Initial Catalog=WangSCM;integrated security=true;";
            using (SqlConnection sqlConn = new SqlConnection(cs))
            {
                sqlConn.Open();
                DataSet ds = new DataSet();

                // load the xml file
                XmlDocument invDoc = new XmlDocument();
                ds.ReadXml(selectedFile);

                DataTable sourceData = ds.Tables["Invoice"];
                DataTable sourceData2 = ds.Tables["InvoiceItem"];


                using (SqlBulkCopy sqlbc = new SqlBulkCopy(sqlConn))
                {
                    sqlbc.DestinationTableName = "Invoice";
                    sqlbc.ColumnMappings.Add("invoiceID", "invoiceID");
                    sqlbc.ColumnMappings.Add("invoiceDate", "invoiceDate");
                    sqlbc.ColumnMappings.Add("orderID", "orderID");
                    sqlbc.ColumnMappings.Add("sellerID", "sellerID");
                    sqlbc.WriteToServer(sourceData);
                }

                using (SqlBulkCopy sqlbc2 = new SqlBulkCopy(sqlConn))
                {
                    sqlbc2.DestinationTableName = "InvoiceItem";
                    sqlbc2.ColumnMappings.Add("invoiceID", "invoiceID");
                    sqlbc2.ColumnMappings.Add("description", "description");
                    sqlbc2.ColumnMappings.Add("quantities", "quantity");
                    sqlbc2.ColumnMappings.Add("totalcost", "totalPrice");
                    sqlbc2.WriteToServer(sourceData2);
                }

                sqlConn.Close();
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<InvoiceData xmlns="http://assignment2.com">
<invoice InvoiceID="1">
<invoicedate>9 May 2015</invoicedate>
<SellerID>001</SellerID>
<BuyerID>001</BuyerID>
<OrderID>001</OrderID>
<ItemID>123</ItemID>
<itemname>CPU</itemname>
<description>This is a central processing unit.</description>
<quantities>50</quantities>
<totalcost>$5000</totalcost>
 </invoice>
</InvoiceData>

为什么我会收到此错误,如何解决?

0 个答案:

没有答案