0

我正在使用以下两个代码读取 csv 文件,但它返回相同的错误。

'E:\business\business\Document\4112013\20580.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

我无法解决这个问题。请帮我解决这个问题

SaveLocation = @"E:\business\business\Document\4112013\20580.csv";
string connStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=@" + SaveLocation + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
// give full path to the file here
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + SaveLocation, conn);
DataSet ds = new DataSet("QueryCSV");
adapter.Fill(ds);
DataTable dt = ds.Tables[0];


System.Data.OleDb.OleDbDataAdapter MyCommand = default(System.Data.OleDb.OleDbDataAdapter);
System.Data.OleDb.OleDbConnection MyConnection = default(System.Data.OleDb.OleDbConnection);
//MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " + "data source=" + SaveLocation + "; " + "Extended Properties=Excel 12.0;");
//MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " + "data source=" + SaveLocation + "; " + "Extended Properties='csv;HDR=Yes;FMT=Delimited(,)';");

MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + SaveLocation + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet5$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
4

2 回答 2

0

You put this as file path, It will work.

string strDataSource = "E:\business\business\Document\4112013\20580.csv";
strDBFile = strDataSource.Substring(0, strDataSource.LastIndexOf('\\'));
mCon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strDBFile + ";Extended Properties=\"Text;Excel 12.0;HDR=No;IMEX=1;FMT=Delimited\"";
于 2013-04-11T07:28:30.970 回答
0

这闻起来更像是安全问题,然后是错误的路径问题。您不能从 ASP.NET Web 应用程序访问硬盘的任何部分。这是出于安全原因,因此潜在的攻击者将无法做同样的事情,以防您的网站安全性受损。

将您的 csv 文件放入您的网络应用程序子目录,并结合以下属性之一访问它:

HttpRuntime.AppDomainAppPath

或与

HttpContext.Current.Request.PhysicalApplicationPath

拥有相对而不是固定的路径会给您带来其他好处,即在将您的应用程序上传到服务器或另一台服务器后,您没有对此进行任何更改。一切都像以前一样继续工作。

教育部信息看这里

于 2013-04-11T07:16:09.583 回答