簡體   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