简体   繁体   中英

SQL Server Bulk Upload in C# no error but not inserting data

I am doing a bulk upload through C#.

It is not a complicated piece of code, but struggling as although the code runs successfully I am not seeing anything on the Database end when I run a query against the destination table.

The table is [awsbillingdetailed_w_res_tags]

public static string BulkUpload(DataTable dt, string tableName, string connectionString)
     {
          string code = "";

//just resizing datatable so that it makes it smaller for testing. Only 5 rows.
          dt = ReduceDataTableSize(dt, 5);

          dt.TableName = tableName;
          string constr = connectionString;
          try
          {
               using (SqlConnection connection = new SqlConnection(constr))
               {
                    connection.Open();
                    //CreatingTranscationsothatitcanrollbackifgotanyerrorwhileuploading
                    SqlTransaction trans = connection.BeginTransaction();
                    //Start bulkCopy
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection,
                    SqlBulkCopyOptions.TableLock |
                    SqlBulkCopyOptions.FireTriggers,
                    trans))
                    {
                         //Fix up default values
                         if(dt.Rows.Count > 0)
                         {
                                for (int i = 0; i < dt.Rows.Count; i++)
                              {
                                   dt.Rows[i]["InvoiceId"] = dt.Rows[i]["InvoiceId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["PayerAccountId"] = dt.Rows[i]["PayerAccountId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["LinkedAccountId"] = dt.Rows[i]["LinkedAccountId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["RecordType"] = dt.Rows[i]["RecordType"].ToString().Replace("\"", "");
                                   dt.Rows[i]["ProductName"] = dt.Rows[i]["ProductName"].ToString().Replace("\"", "");
                                   dt.Rows[i]["RateId"] = dt.Rows[i]["RateId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["SubscriptionId"] = dt.Rows[i]["SubscriptionId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["PricingplanId"] = dt.Rows[i]["PricingplanId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["UsageType"] = dt.Rows[i]["UsageType"].ToString().Replace("\"", "");
                                   dt.Rows[i]["PricingplanId"] = dt.Rows[i]["PricingplanId"].ToString().Replace("\"", "");
                                   dt.Rows[i]["Operation"] = dt.Rows[i]["Operation"].ToString().Replace("\"", "");
                                   dt.Rows[i]["AvailabilityZone"] = dt.Rows[i]["AvailabilityZone"].ToString().Replace("\"", "");
                                   dt.Rows[i]["ReservedInstance"] = dt.Rows[i]["ReservedInstance"].ToString().Replace("\"", "");
                                      dt.Rows[i]["ItemDescription"] = dt.Rows[i]["ItemDescription"].ToString().Replace("\"", "");
                                      dt.Rows[i]["ResourceId"] = dt.Rows[i]["ResourceId"].ToString().Replace("\"", "");
                                      dt.Rows[i]["RecordId"] = dt.Rows[i]["RecordId"].ToString().Replace("\"", "");

                                   dt.Rows[i]["UsageQuantity"] = dt.Rows[i]["UsageQuantity"].ToString().Replace("\"", "");
                                   dt.Rows[i]["usagestartdate"] = dt.Rows[i]["usagestartdate"].ToString() == "" ? DateTime.Now.AddYears(-2014) : Convert.ToDateTime((dt.Rows[i]["usagestartdate"].ToString().Replace("\"", "")));
                                   dt.Rows[i]["usageenddate"] = dt.Rows[i]["usageenddate"].ToString() == "" ? DateTime.Now.AddYears(-2014) : Convert.ToDateTime((dt.Rows[i]["usageenddate"].ToString().Replace("\"", "")));
                                   dt.Rows[i]["UsageQuantity"] = dt.Rows[i]["UsageQuantity"].ToString().Replace("\"", "");
                                   dt.Rows[i]["BlendedRate"] = dt.Rows[i]["BlendedRate"].ToString().Replace("\"", "");
                                   dt.Rows[i]["BlendedCost"] = dt.Rows[i]["BlendedCost"].ToString().Replace("\"", "");
                                   dt.Rows[i]["UnBlendedRate"] = dt.Rows[i]["UnBlendedRate"].ToString().Replace("\"", "");
                                   dt.Rows[i]["UnBlendedCost"] = dt.Rows[i]["UnBlendedCost"].ToString().Replace("\"", "");


                              }
                         }

                        bulkCopy.DestinationTableName = tableName;
                        bulkCopy.ColumnMappings.Add("InvoiceId", "invoiceid");
                        bulkCopy.ColumnMappings.Add("PayerAccountId", "payeraccountid");
                        bulkCopy.ColumnMappings.Add("LinkedAccountId", "linkedaccountid");
                        bulkCopy.ColumnMappings.Add("RecordType", "recordtype");
                        bulkCopy.ColumnMappings.Add("ProductName", "productname");
                        bulkCopy.ColumnMappings.Add("RateId", "rateid");
                        bulkCopy.ColumnMappings.Add("SubscriptionId", "subscriptionid");
                        bulkCopy.ColumnMappings.Add("PricingplanId", "pricingplanid");
                        bulkCopy.ColumnMappings.Add("UsageType", "usagetype");
                        bulkCopy.ColumnMappings.Add("Operation", "operation");
                        bulkCopy.ColumnMappings.Add("AvailabilityZone", "availabilityzone");
                        bulkCopy.ColumnMappings.Add("ReservedInstance", "reservedinstance");
                        bulkCopy.ColumnMappings.Add("ItemDescription", "itemdescription");
                        bulkCopy.ColumnMappings.Add("UsageStartDate", "usagestartdate");
                        bulkCopy.ColumnMappings.Add("UsageEndDate", "usageenddate");
                       bulkCopy.ColumnMappings.Add("UsageQuantity", "usagequantity");
                        bulkCopy.ColumnMappings.Add("BlendedRate", "blendedrate");
                        bulkCopy.ColumnMappings.Add("BlendedCost", "blendedcost");
                        bulkCopy.ColumnMappings.Add("UnBlendedRate", "unblendedrate");
                        bulkCopy.ColumnMappings.Add("UnBlendedCost", "unblendedcost");
                        bulkCopy.ColumnMappings.Add("resourceid", "resourceid");

                         //write the data in the "dataTable"
                         bulkCopy.WriteToServer(dt);

                         code = "Data Processed Successfully";
                    }
               }
               return code;
          }



          catch (Exception ex)
          {
              code = "Error while processing file";
              System.Diagnostics.Debug.Write("EXCEPTION BulkUpload: " + ex.ToString());
          }

          return code;
     }

I have also double checked to ensure there were no double quotes or any blank fields for now.

I get no errors, but nothing actually gets into the DB. Any ideas why? Lost a few hours trying to debug this but cannot see any issues. Read plenty of forums but lead me no where. Your help is much appreciated.

Thanks in advance.

Ok, just realised that I did not finished/committed the transaction. Funny how we need to write the code , cry for help and then we just suddenly get it.

Essentially missing...

trans.Commit();

after the bulk upload. Thanks and apologies for wasting ur time :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM