Microsoft Jet数据库引擎无法打开该文件

时间:2014-02-17 20:43:01

标签: asp.net sql csv

我正在编写一个代码来查询.CSV文件使用下面的SQL是我的代码,它完全正常

string fileDirectory = @"C:\TechnicalTest\GskTest\Csv\SampleData.csv";
string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
            + fileDirectory + ";Extended Properties='text;HDR=YES;'";

string sqlCust = "Select Count(order_id), order_id, contact_id from SampleData.csv "
+ "Group by order_id, contact_id "
+ "Order by 1 Desc";

string sqlProd = "Select Count(order_id), product_id from SampleData.csv "
+ "Group by product_id "
+ "Order by 1 Desc";

string sqlOrders = "Select Count(order_id) from SampleData.csv "
+"Order by 1 Desc";

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(fileDirectory) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
con.Open();

OleDbDataAdapter daCust = new OleDbDataAdapter(sqlCust, con);
DataTable dtCust = new DataTable();
daCust.Fill(dtCust);

OleDbDataAdapter daProd = new OleDbDataAdapter(sqlProd, con);
DataTable dtProd = new DataTable();
daProd.Fill(dtProd);

OleDbDataAdapter daOrders = new OleDbDataAdapter(sqlOrders, con);
DataTable dtOrders = new DataTable();
daOrders.Fill(dtOrders);

con.Close();

但是当我尝试通过传递从asp.net文件上传控件检索的文件路径从函数调用相同的代码时,它不起作用。请参阅下面的代码。

   protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (fupPath.HasFile)
        {

            string filename = Path.GetFileName(fupPath.FileName);
            String csv_file_path = Path.Combine(Server.MapPath("~/Csv"), filename);
            fupPath.SaveAs(csv_file_path);

            Summery(csv_file_path);

            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
            Response.Write("Rows count:" + csvData.Rows.Count);
            //dtSummary(csvData);
        }
    }

    protected void Summery(string fileName)
    {
        //string fileDirectory = @"C:\TechnicalTest\GskTest\Csv\SampleData.csv";

        string fileDirectory = fileName;
        //string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
        //            + System.IO.Path.GetDirectoryName(fileDirectory) + ";Extended Properties='text;HDR=YES;FMT=Delimited\'";

        string sqlCust = "Select Count(order_id), order_id, contact_id from SampleData.csv "
        + "Group by order_id, contact_id "
        + "Order by 1 Desc";

        string sqlProd = "Select Count(order_id), product_id from SampleData.csv "
        + "Group by product_id "
        + "Order by 1 Desc";

        string sqlOrders = "Select Count(order_id) from SampleData.csv "
        + "Order by 1 Desc";

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(fileDirectory) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
        //OleDbConnection conn = new OleDbConnection(strCSVConnString);
        conn.Open();

        OleDbDataAdapter daCust = new OleDbDataAdapter(sqlCust, conn);
        DataTable dtCust = new DataTable();
        daCust.Fill(dtCust);
        daCust.Dispose();

        OleDbDataAdapter daProd = new OleDbDataAdapter(sqlProd, conn);
        DataTable dtProd = new DataTable();
        daProd.Fill(dtProd);
        daProd.Dispose();

        OleDbDataAdapter daOrders = new OleDbDataAdapter(sqlOrders, conn);
        DataTable dtOrders = new DataTable();
        daOrders.Fill(dtOrders);
        daOrders.Dispose();

        conn.Close();
    }

1 个答案:

答案 0 :(得分:0)

您需要调用工作表名称SheetName$而不是文件名SampleData.csv

例如,

Select Count(order_id), order_id, contact_id from [SheetName$]

通常,以下是在ASP.Net中获取文件路径的方法,因为您不知道托管Web应用程序的驱动器号。此外,您无权访问位于Web应用程序之外的文件。

var filePath = string.Format("{0}App_Data\\ExportImport\\{1}",
   HttpRuntime.AppDomainAppPath, "SampleData.csv");