我正在尝试使用MVC5中的Epplus库和C#中的Entity Framework 6将文件导入excel数据库并导入excel数据库。我收到此错误。我知道获取连接字符串的方式不正确。
我在“连接”上等待新的批量写入器行上收到错误消息
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中的记录。我想将此类用于我的代码以节省时间。
答案 0 :(得分:1)
由于您的excelImportDBEntities
变量的类型为SqlConnection
,因此它具有Database
属性。该属性返回字符串-此连接连接到的数据库的名称。当然,您无法从数据库名称中获得.Connection
,并且尝试时会出现编译错误。
然后,如何解决它。您的BulkWriter
的{{1}}方法在这里需要InsertAsync
对象,并且您已经有了该对象SqlConnection
。为什么不直接使用它?替换
excelImportDBEntities
与
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);
,此错误应该消失了。
另一个问题是,如其他答案和注释中所述,您似乎对连接字符串的处理不当,这将在编译和运行代码后产生错误。
答案 1 :(得分:0)
尝试这个:
string connectionString = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"].ConnectionString;
using (SqlConnection excelImportDBEntities = new SqlConnection(connectionString)){
...
}