将Excel导入SQL不会导入正确数量的记录

时间:2019-05-09 20:28:32

标签: c# sql excel

我发生了一件非常奇怪的事情,我无法弄清原因。我正在使用OleDbConnection将电子表格导入数据表。然后,我通过将数据表行读取到SQL数据库中来插入数据。奇怪的是,当我以调试模式从本地计算机到生产数据库中执行此操作时,这似乎工作正常。示例:电子表格有733行,并且导入了733行。但是,当我将应用程序部署到生产环境并在相同的数据库中运行相同的代码时,只会导入496行。 Doeas对此有任何想法吗?

for %files.sort: *.value.kv  -> ($key, $value) {
private readonly FulfillmentContext db = new FulfillmentContext();
        private readonly string username = HttpContext.Current.User.Identity.GetUserName();
        private readonly DataTable dt = new DataTable();
        readonly UserRepository userRepository = new UserRepository();
public void ImportLocations(HttpPostedFileBase file)
{
            string filePath = string.Empty;
            string fileName = string.Empty;
            string GUI = Guid.NewGuid().ToString();

            if (file != null)
            {
                string path = HttpContext.Current.Server.MapPath("~/Content/Upload/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(file.FileName);
                fileName = Path.GetFileName(file.FileName);
                string extension = Path.GetExtension(file.FileName);
                file.SaveAs(filePath);

                string conString = string.Empty;
                switch (extension)
                {
                    case ".xls": //Excel 97-03.
                        conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        break;
                    case ".xlsx": //Excel 07 and above.
                        conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        break;
                }

                conString = string.Format(conString, filePath);

                using (OleDbConnection connExcel = new OleDbConnection(conString))
                {
                    using (OleDbCommand cmdExcel = new OleDbCommand())
                    {
                        using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                        {
                            cmdExcel.Connection = connExcel;

                            //Get the name of First Sheet.
                            connExcel.Open();
                            DataTable dtExcelSchema;
                            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                            connExcel.Close();

                            //Read Data from First Sheet.
                            connExcel.Open();
                            cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
                            odaExcel.SelectCommand = cmdExcel;
                            odaExcel.Fill(dt);                            
                            connExcel.Close();
                        }
                    }
                }

                InsertRecords(fileName);
            }
}

0 个答案:

没有答案