我正在通過C#進行批量上傳。C#中的SQL Server批量上傳沒有錯誤,但沒有插入數據
這不是一段複雜的代碼,但儘管代碼能夠成功運行,但我苦苦掙扎當我對目標表運行查詢時,數據庫一端沒有看到任何東西。
表是 [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;
}
我也雙重檢查,以確保沒有雙引號或任何空白字段現在。
我沒有得到任何錯誤,但沒有任何實際進入數據庫。任何想法爲什麼?嘗試調試了幾個小時,但看不到任何問題。閱讀大量的論壇,但沒有帶領我去哪裏。非常感謝您的幫助。
在此先感謝。
你沒有提交你的交易。請查看「使用現有的tranactions」部分@ http://msdn.microsoft.com/en-us/library/tchktcdk%28v=vs.110%29.aspx – dugas