SQL批量插入不保存数据库中的所有行

时间:2015-05-22 06:30:52

标签: asp.net sql-server

我正在使用SQL Bulkcopy将excel数据导入SQl Server。它在我的本地系统上运行良好(它一次可以节省超过80,000行)。 但是当我在网上上传它时,它无法保存数据库中的所有行。 我使用以下代码:

SqlBulkCopy bulkInsert = new SqlBulkCopy(con);
bulkInsert.DestinationTableName = "BrucknerData";
bulkInsert.ColumnMappings.Add(7, "Customer");
bulkInsert.ColumnMappings.Add(28, "InvoiceDate");
bulkInsert.ColumnMappings.Add(27, "InvoiceNo");
bulkInsert.ColumnMappings.Add(23, "CustomerPo");
bulkInsert.ColumnMappings.Add(26, "SoLine");
bulkInsert.ColumnMappings.Add(11, "VendorName");
bulkInsert.ColumnMappings.Add(12, "Category");
bulkInsert.ColumnMappings.Add(13, "Item");
bulkInsert.ColumnMappings.Add(17, "PickQty");
bulkInsert.ColumnMappings.Add(18, "Price");
bulkInsert.ColumnMappings.Add(19, "PriceExtention");
con.Open();
bulkInsert.BulkCopyTimeout = 3600;
bulkInsert.WriteToServer(dtExcelData);
con.Close();

请帮我解决。

1 个答案:

答案 0 :(得分:0)

参考此代码。

 if (FileUploadControl.HasFile)
    {
        try
        {
            if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || FileUploadControl.PostedFile.ContentType == "application/vnd.ms-excel")
            {
                if (FileUploadControl.PostedFile.ContentLength >= 0)
                {
                    string filename = Path.GetFileNameWithoutExtension(FileUploadControl.FileName);
                    string fileExt = Path.GetExtension(FileUploadControl.FileName);

                    FileUploadControl.SaveAs(Server.MapPath("~/DownloadedExcelFilesOp4/myfile" + fileExt));
                    if (FileUploadControl.PostedFile.ContentLength == 0)
                    {
                        lblMsg.Text = fun.UploadStatus;
                        lblMsg.ForeColor = Color.Green;
                    }
                    else
                        lblMsg.Text = fun.UploadStatus;
                    lblMsg.ForeColor = Color.Green;
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", Server.MapPath(@"~\DownloadedExcelFilesOp4\myfile" + fileExt));// + "\\" + FileUploadControl.PostedFile.FileName.ToString());
                    using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
                    {
                        OleDbCommand command = new OleDbCommand(("Select [Customer] ,[InvoiceDate] , [InvoiceNo] , [CustomerPo],[SoLine] ,[VendorName] ,[Category] ,[Item] ,[PickQty] ,[Price] ,[PriceExtentio]  FROM [Sheet1$]"), connection);
                        connection.Open();
                        using (DbDataReader dr = command.ExecuteReader())
                        {
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
                            {
                                bulkCopy.DestinationTableName = "TempExamQuation4Master";
                                bulkCopy.ColumnMappings.Add("Customer", "Customer");
                                bulkCopy.ColumnMappings.Add("InvoiceDate", "InvoiceDate");
                                bulkCopy.ColumnMappings.Add("InvoiceNo", "InvoiceNo");
                                bulkCopy.ColumnMappings.Add("CustomerPo", "CustomerPo");
                                bulkCopy.ColumnMappings.Add("SoLine", "SoLine");
                                bulkCopy.ColumnMappings.Add("VendorName", "VendorName");
                                bulkCopy.ColumnMappings.Add("Categor", "Categor");
                                bulkCopy.ColumnMappings.Add("Item", "Item");
                                bulkCopy.ColumnMappings.Add("PickQty", "PickQty");
                                bulkCopy.ColumnMappings.Add("Price", "Price");
                                bulkCopy.ColumnMappings.Add("PriceExtentio", "PriceExtentio");

                                bulkCopy.WriteToServer(dr);
                            }
                        }
                    }