c# reading csv file gives not a valid path

前端 未结 7 1892
Happy的楠姐
Happy的楠姐 2020-12-05 07:04

I can\'t seem to read a .csv file using the following connection string:

var fileName = string.Format(\"{0}{1}\", AppDomain.CurrentDomain.BaseDirectory, \"Up         


        
7条回答
  •  遥遥无期
    2020-12-05 07:46

    Ok, I dug a little further and it seems that my connection string is wrong. With CSV files, you don't specify the actual file name but the directory where it belongs, eg.

    var fileName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Uploads\\");
    string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""text;HDR=YES;FMT=Delimited""", fileName);
    OleDbConnection oledbConn = new OleDbConnection(connectionString);
    oledbConn.Open();
    var cmd = new OleDbCommand("SELECT * FROM [countrylist.csv]", oledbConn);
    

    And you specify the filename in the SelectCommand. What a strange way of doing it. It's working for me now.

提交回复
热议问题