I am trying to insert data from a CSV file into a table in a local Access database but the CSV file is in a different location than my database. The insert statement is trying to insert a CSV file that is located in the Windows temporary folder but I keep getting an error saying that the object (CSV file) could not be found. Help would be appreciated, thank you!
Below is the error that I am receiving:
The Microsoft Access database engine could not find the object 'S_M_85010747_201605.csv'. Make sure the object exists and that you spell its name and the path name correctly. If 'S_M_85010747_201605.csv' is not a local object, check your network connection or contact the server administrator.
Below is my code:
//CSV file name
string csvFile = ddlReportType.Text.Substring(0, 1) + "_"
+ ddlDateType.Text.Substring(0, 1) + "_" + lblVendorID.Text + "_" + txtDate.Text + ".csv";
//Path for temporary folder
string marFolder = Path.GetTempPath() + @"\MobileAppReports";
//CSV file in temporary folder path
string marCSVfolderPath = marFolder + @"\" + ddlReportType.Text.Substring(0, 1) + "_"
+ ddlDateType.Text.Substring(0, 1) + "_" + lblVendorID.Text + "_" + txtDate.Text + ".csv";
var fileNameToInsert = Path.GetFileName(marCSVfolderPath);
if (File.Exists(marCSVfolderPath))
{
OleDbCommand cmdBulk = new OleDbCommand(@"INSERT INTO SalesSummary" +
@"SELECT * FROM [Text;FMT=Delimited;HDR=Yes;ACCDB=Yes;Database=C:\Desktop].[" + fileNameToInsert + "]", MyConn);
MyConn.Open();
cmdBulk.ExecuteNonQuery();
MyConn.Close();
Directory.Delete(marFolder, true);//Deletes the csv file in the temp folder
}
else
{
MessageBox.Show(csvFile + " does not exist in the temp folder.");
}
I can see you define the csv file path and the temp file path, but i dont see where you copy the csv to the temp path so got no idea how it passes the File.Exists stage.
Also, your query string is only defining the fileNameToInsert and not the whole path. As others have stated, it also looks like your are defining the database path as the csv file.
Is this all of the code? Really feels like you missed something here.