简体   繁体   English

将Excel数据导入SQL Server 2014表的Asp.net Web页面(剃须刀)页面

[英]Asp.net web pages (razor) page to import Excel Data to SQL Server 2014 table

Background: I have a asp.net web pages (razor) application on a 2008R2 web server that has a page with an excel upload button which allows the user to post excel data to a SQL Server 2014 table. 背景:我在2008R2 Web服务器上有一个asp.net网页(剃须刀)应用程序,该应用程序的页面带有excel上传按钮,允许用户将excel数据发布到SQL Server 2014表。 In order to get it to work on the dev server I had to install the Microsoft Access Database Engine 2010 as outlined in this MikesDotNetting blog post. 为了使其能够在开发服务器上工作,我必须安装此MikesDotNetting博客文章中概述的Microsoft Access Database Engine 2010 However, I have been encouraged to find a way to import data to the production server that doesn't require installing Microsoft Access Database Engine on the server. 但是,鼓励我找到一种无需在服务器上安装Microsoft Access Database Engine即可将数据导入生产服务器的方法。 I have a C# Windows Forms app that does the same thing but I am not good at converting Windows Forms app code to my razor web sites. 我有一个执行相同操作的C#Windows窗体应用程序,但是我不擅长将Windows窗体应用程序代码转换为我的剃刀网站。

I actually didn't expect this code below to work since I am posting data to SQL Server 2014 and according to MS OleDB has been deprecated and has not been supported since SQL Server 2012. 实际上,我并不希望下面的代码能正常工作,因为我正在将数据发布到SQL Server 2014,并且据MS OleDB所述,自SQL Server 2012起不推荐使用 ,并且不予支持。

Q1 - Does deprecated just mean 'use at your own risk' or does it mean it shouldn't work on 2014 at all? 问题1- 是否已弃用只是意味着“使用风险自负”,还是意味着它完全不适用于2014年?

The code below (which occurs right after my if...ISPOST) works great for getting an excel file uploaded to my web server then inserted into a SQL Server 2014 table. 下面的代码(在我的if ... ISPOST之后立即发生)非常适合将excel文件上传到我的Web服务器,然后插入到SQL Server 2014表中。 I just need to find a way to make it work without installing Microsoft Access Database Engine to the production server 2008 R2 IIS7. 我只需要找到一种无需将Microsoft Access数据库引擎安装到生产服务器2008 R2 IIS7即可使其工作的方法。

Q2 - Is there some Nuget Package or other workaround that might help? 问题 2-是否有一些Nuget软件包或其他解决方法可能会有所帮助?

Q3 - Is there a quick fix to convert that OLE DB connection string to ODBC? 问题3- 是否有快速解决方案将OLE DB连接字符串转换为ODBC?

Q4 - Is there a namespace / assembly I could add to the project that would not require the access database engine install on the server? 问题4- 是否可以将不需要在服务器上安装访问数据库引擎的命名空间/程序集添加到项目中?

Code: 码:

try
                {
                    // import code
                    var excel = Request.Files[0];
                    var file = Path.Combine(Server.MapPath("~/Upload_Folder/"), excel.FileName);
                    excel.SaveAs(file);
                    var excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=1\"";
                    var sqlConnectionString = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ToString();
                    var excelData = new DataTable();
                    using (var myConnection = new OleDbConnection(string.Format(excelConnectionString, file)))
                    {
                        var myCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", myConnection);
                        var myAdapter = new OleDbDataAdapter(myCommand);
                        myAdapter.Fill(excelData);
                        using (var destinationConnection = new SqlConnection(sqlConnectionString))
                        {
                            destinationConnection.Open();
                            using (var bulkCopy = new SqlBulkCopy(destinationConnection))
                            {
                                bulkCopy.DestinationTableName = "MYTABLE";
                                bulkCopy.ColumnMappings.Add("CODE", "code");
                                bulkCopy.ColumnMappings.Add("TITLE", "title");                                   
                                bulkCopy.ColumnMappings.Add("LAST_NAME", "last_name");
                                bulkCopy.ColumnMappings.Add("FIRST_NAME", "first_name");
                            }
                        }
                        if (Directory.Exists(Path.GetDirectoryName(file)))
                        {
                            File.Delete(file);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

I didn't get any responses to this question after nearly 50 views which made me think it was too general a question with many different solutions. 经过将近50次意见之后,我没有对此问题做出任何回应,这让我认为使用许多不同的解决方案都太笼统了。 However to make it as useful a post as possible I just wanted to document my own solutions (with links) to the 4 original questions proposed (see comments 1-4 above). 但是,为了使帖子尽可能有用,我只想记录我自己的解决方案(带有链接),以解决提出的4个原始问题(请参见上面的注释1-4)。 --Tim -蒂姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM