【发布时间】:2019-10-06 19:56:44
【问题描述】:
我正在尝试使用 MVC5 中的 Epplus 库和 C# 中的实体框架 6 将文件导入我的 sql 数据库。我收到此错误。我知道我获取连接字符串的方式不对。
我在“连接”上的 await new bulkwriter 行上收到错误
public async Task<ActionResult> StructureAsync(FormCollection postedFile)
{
var usersList = new List<bomStructuredImportTgt>();
if (Request != null)
{
HttpPostedFileBase file = Request.Files["postedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
using (var package = new ExcelPackage(file.InputStream))
{
var currentSheet = package.Workbook.Worksheets;
var workSheet = currentSheet.First();
var noOfCol = workSheet.Dimension.End.Column;
var noOfRow = workSheet.Dimension.End.Row;
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
var user = new bomStructuredImportTgt();
user.ACTUAL_DATE = Convert.ToDateTime(workSheet.Cells[rowIterator, 1].Value);
user.DESCRIPTION = workSheet.Cells[rowIterator, 2].Value?.ToString();
user.LEVEL = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
user.PARENT_PARTNO = workSheet.Cells[rowIterator, 4].Value?.ToString();
user.PART_NO = workSheet.Cells[rowIterator, 5].Value?.ToString();
user.PART_NAME = workSheet.Cells[rowIterator, 6].Value?.ToString();
user.HNS = workSheet.Cells[rowIterator, 7].Value?.ToString();
user.DWGSZ = workSheet.Cells[rowIterator, 8].Value?.ToString();
user.PART = workSheet.Cells[rowIterator, 9].Value?.ToString();
user.L1QTY = Convert.ToInt32(workSheet.Cells[rowIterator, 10].Value);
user.COLORM = workSheet.Cells[rowIterator, 11].Value?.ToString();
user.ATTCD = workSheet.Cells[rowIterator, 12].Value?.ToString();
user.KD = workSheet.Cells[rowIterator, 13].Value?.ToString();
user.SELL = workSheet.Cells[rowIterator, 14].Value?.ToString();
user.PL_GROUP = workSheet.Cells[rowIterator, 15].Value?.ToString();
user.PL1 = workSheet.Cells[rowIterator, 16].Value?.ToString();
user.AT1 = workSheet.Cells[rowIterator, 17].Value?.ToString();
user.PL2 = workSheet.Cells[rowIterator, 18].Value?.ToString();
user.AT2 = workSheet.Cells[rowIterator, 19].Value?.ToString();
user.PL3 = workSheet.Cells[rowIterator, 20].Value?.ToString();
user.PLANT = workSheet.Cells[rowIterator, 21].Value?.ToString();
user.SHRPCMINMAX = workSheet.Cells[rowIterator, 22].Value?.ToString();
usersList.Add(user);
}
}
}
}
using (SqlConnection excelImportDBEntities = new SqlConnection("Dev_Purchasing_New_ModelEntities"))
{
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);
}
return View("Structure");
}
public class BulkWriter
{
private static readonly ConcurrentDictionary<Type, SqlBulkCopyColumnMapping[]> ColumnMapping =
new ConcurrentDictionary<Type, SqlBulkCopyColumnMapping[]>();
public async Task InsertAsync<T>(IEnumerable<T> items, string bomStructuredImportTgt, SqlConnection excelImportDBEntities,
CancellationToken cancellationToken)
{
using (var bulk = new SqlBulkCopy(excelImportDBEntities))
using (var reader = ObjectReader.Create(items))
{
bulk.DestinationTableName = bomStructuredImportTgt;
foreach (var colMap in GetColumnMappings<T>())
bulk.ColumnMappings.Add(colMap);
await bulk.WriteToServerAsync(reader, cancellationToken);
}
}
private static IEnumerable<SqlBulkCopyColumnMapping> GetColumnMappings<T>() =>
ColumnMapping.GetOrAdd(typeof(T),
type =>
type.GetProperties()
.Select(p => new SqlBulkCopyColumnMapping(p.Name, p.Name)).ToArray());
}
我有一个 BulkWriter 类,用于批量复制 sql 中的记录。我想使用这个类来提高我的代码的时间效率。
【问题讨论】:
-
什么是
Dev_Purchasing_New_ModelEntities?如果是web.config中的连接字符串的名称,则应自行检索:ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"] -
是的,它是连接字符串中的名称\
-
这样吗?使用 (SqlConnection excelImportDBEntities = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"]) { 等待新 BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None); } 返回视图(“结构”);
-
至于为什么您会收到该特定错误,
excelImportDBEntities.Database是string类型。strings 没有Connection属性。 -
我也试过这样,var conString = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"].ConnectionString;使用 (SqlConnection excelImportDBEntities = new SqlConnection(conString)) { await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", ((excelImportDBEntities).Database.Connection), CancellationToken.None); }
标签: c# asp.net-mvc-5 entity-framework-6 epplus sqlconnection