'string' does not contain a definition for 'Connection'

asp.net-mvc-5 c# entity-framework-6 epplus sqlconnection


I'm attempting to import an excel file using the Epplus library in MVC5 and the Entity framework 6 in C# into my SQL database. I experience this error. I am aware that the method through which I am obtaining my connection string is incorrect.

I am seeing an issue on the 'Connection' await new bulkwriter line.

 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();

        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>())
                await bulk.WriteToServerAsync(reader, cancellationToken);

        private static IEnumerable<SqlBulkCopyColumnMapping> GetColumnMappings<T>() =>
                type =>
                        .Select(p => new SqlBulkCopyColumnMapping(p.Name, p.Name)).ToArray());

I have a class called BulkWriter that is used to bulk copy the sql data. For my code to run quickly, I wanted to utilize this class.

5/20/2019 12:28:59 PM

Accepted Answer

due toexcelImportDBEntities the variable is aSqlConnection , it hasDatabase property. This parameter returns the name of the database that this connection connects to, which is string. Obviously, you cannot get.Connection from the database name, however attempting to do so results in compilation errors.

How to repair it after that. YourBulkWriter 's InsertAsync method anticipatesSqlConnection item right here, and you already own it,excelImportDBEntities Why not use it straight away? Replace

await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);


await new BulkWriter().InsertAsync(usersList, "bomStructuredImportTgt", excelImportDBEntities, CancellationToken.None);

and this mistake need to be fixed.

Another problem is that, as other answers and comments have pointed out, you seem to have handled your connection string incorrectly, which will result in an error after your code has been compiled and executed.

5/20/2019 1:36:29 PM

Popular Answer

Attempt this one:

string connectionString = ConfigurationManager.ConnectionStrings["Dev_Purchasing_New_ModelEntities"].ConnectionString;
using (SqlConnection excelImportDBEntities = new SqlConnection(connectionString)){

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow