繁体   English   中英

打开ExcelConnection C#VS2005时出错

[英]Error in opening ExcelConnection C# VS2005

我正在尝试将.csv文件导入我的数据库。 我可以将excel工作表导入到我的数据库中,但由于.csv的文件格式与.xls不同,我需要专门为.csv创建一个导入功能。

以下是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
    // Get the name of the Excel spreadsheet to upload. 
    string strFileName = Server.HtmlEncode(FileUpload1.FileName);

    // Get the extension of the Excel spreadsheet. 
    string strExtension = Path.GetExtension(strFileName);

    // Validate the file extension. 
    if (strExtension != ".xls" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
    {
        Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
        return;
    }

                // Generate the file name to save. 
        string strUploadFileName = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;

        // Save the Excel spreadsheet on server. 
        FileUpload1.SaveAs(strUploadFileName);

        // Create Connection to Excel Workbook
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Text;";
        using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)){
        OleDbCommand ExcelCommand = new OleDbCommand("SELECT [columns] FROM +userrolelist", ExcelConnection);

        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);

        ExcelConnection.Open();

    using (DbDataReader dr = ExcelCommand.ExecuteReader())
    {
        // SQL Server Connection String
        string sqlConnectionString = "Data Source=<IP>;Initial Catalog=<DB>;User ID=<userid>;Password=<password>";

        // Bulk Copy to SQL Server
        using (SqlBulkCopy bulkCopy =
                   new SqlBulkCopy(sqlConnectionString))
        {
            bulkCopy.DestinationTableName = "DEMUserRoles";
            bulkCopy.WriteToServer(dr);
            Response.Write("<script>alert('DEM User Data imported');</script>");

        }
    }
    }
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");

}

该文件已成功保存,但错误表明该文件的路径无效,即使该文件已成功保存为.csv,因此我无法继续将数据导入我的数据库的过程。

以下是我的错误的屏幕截图: 在此输入图像描述在此输入图像描述

总之,我遇到的错误是csv文件保存的文件路径无效,尽管csv文件已成功保存。 需要经验丰富的帮助。 谢谢

我强烈建议您不要使用OLE来访问Excel文档。 有无数的故障和错误。 通常,当一列的不同单元格可以包含不同的数据类型时,使用sql访问数据 - 是无稽之谈。 但即使没有这个,也有足够的错误。

使用COM对象的Excel。 否则,你会很难相信我在场:-)

连接字符串数据源应仅包含CSV文件的路径 它不应包含CSV文件名。

文件名在SQL语句中指定为表。

string dir = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\";
string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;

string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";

OleDbCommand ExcelCommand = new OleDbCommand(
    "SELECT [columns] FROM " + mycsv, ExcelConnection);

暂无
暂无

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

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