我正在通过C#进行批量上传。
这不是一段复杂的代码,但是虽然代码运行成功,但是当我对目标表运行查询时,我在数据库端没有看到任何内容。
表是 [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;
}
我还进行了双重检查以确保现在没有双引号或任何空白字段。
我没有错误,但实际上没有任何内容进入数据库。有什么想法吗?试图调试这几个小时但是看不到任何问题。阅读大量的论坛,但没有引导我。非常感谢您的帮助。
提前致谢。
答案 0 :(得分:3)
好的,刚才意识到我没有完成/提交交易。有趣的是我们需要编写代码,请求帮助然后我们突然得到它。
基本上缺少......
trans.Commit();
批量上传后。谢谢,道歉浪费你的时间:)