且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#将excel文件表导入sql数据库报错

更新时间:2023-01-21 16:27:14

看起来您的连接字符串实际上有几个问题.一方面,Excel 连接字符串不应包含初始目录",它们应包含引用文件的数据源,而不是服务器.

It looks like you actually have several things wrong with your connection strings. For one thing, Excel connection strings should not include an "Initial Catalog", and they should include a Data Source referring to the file, not a server.

试试这个:

        // There is no column name In a Excel spreadsheet.  
        // So we specify "HDR=YES" in the connection string to use  
        // the values in the first row as column names.  
        if (strExtension == ".xls")
        {
            // Excel 97-2003 
            strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strUploadFileName) + ";Extended Properties=\"Excel 8.0;HDR=Yes;\"";

            //if the above doesn't work, you may need to prefix OLEDB; to the string, e.g.
            //strExcelConn = "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strUploadFileName) + ";Extended Properties=\"Excel 8.0;HDR=Yes;\"";
        }
        else
        {
            // Excel 2007 
            strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(strUploadFileName) + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
        }

仅供参考,http://connectionstrings.com 是处理此类事情的好资源.

Fyi, http://connectionstrings.com is a good resource for these kind of things.