'string' 不包含 'Connection' 的定义
'string' does not contain a definition for 'Connection'
我正在尝试使用 MVC5 中的 Epplus 库和 C# 中的 Entity framework 6 将 excel 文件导入到我的 sql 数据库中。我收到此错误。我知道我获取连接字符串的方式不对。
我在 'Connection'
的 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());
}
我有一个 class BulkWriter,用于批量复制 sql 中的记录。我想使用这个 class 让我的代码更省时。
试试这个:
string connectionString = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"].ConnectionString;
using (SqlConnection excelImportDBEntities = new SqlConnection(connectionString)){
...
}
由于您的 excelImportDBEntities
变量是 SqlConnection
类型,它有 Database
属性。 属性 returns a string - 此连接连接到的数据库的名称。当然,您不能从数据库名称中获取 .Connection
并且在尝试时会出现编译错误。
然后,如何修复它。您的 BulkWriter
的 InsertAsync
方法需要 SqlConnection
对象,而您已经有了这个对象 excelImportDBEntities
。为什么不直接使用呢?替换
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);
和
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities, CancellationToken.None);
这个错误应该消失了。
另一个问题是,正如其他答案和评论中提到的,您似乎对连接字符串处理不当,这将在您编译和 运行 您的代码后产生错误。
我正在尝试使用 MVC5 中的 Epplus 库和 C# 中的 Entity framework 6 将 excel 文件导入到我的 sql 数据库中。我收到此错误。我知道我获取连接字符串的方式不对。
我在 'Connection'
的 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());
}
我有一个 class BulkWriter,用于批量复制 sql 中的记录。我想使用这个 class 让我的代码更省时。
试试这个:
string connectionString = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"].ConnectionString;
using (SqlConnection excelImportDBEntities = new SqlConnection(connectionString)){
...
}
由于您的 excelImportDBEntities
变量是 SqlConnection
类型,它有 Database
属性。 属性 returns a string - 此连接连接到的数据库的名称。当然,您不能从数据库名称中获取 .Connection
并且在尝试时会出现编译错误。
然后,如何修复它。您的 BulkWriter
的 InsertAsync
方法需要 SqlConnection
对象,而您已经有了这个对象 excelImportDBEntities
。为什么不直接使用呢?替换
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);
和
await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities, CancellationToken.None);
这个错误应该消失了。
另一个问题是,正如其他答案和评论中提到的,您似乎对连接字符串处理不当,这将在您编译和 运行 您的代码后产生错误。