将 csv 文件插入到位于不同位置的 Access 数据库中 C#
Inserting a csv file into an Access database that is in a different location C#
我正在尝试将 CSV 文件中的数据插入本地 Access 数据库中的 table,但 CSV 文件与我的数据库位于不同的位置。 insert 语句试图插入一个位于 Windows 临时文件夹中的 CSV 文件,但我一直收到一条错误消息,提示找不到该对象(CSV 文件)。不胜感激,谢谢!
以下是我收到的错误:
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.
下面是我的代码:
//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.");
}
我可以看到您定义了 csv 文件路径和临时文件路径,但我没有看到您将 csv 复制到临时路径的位置,所以不知道它是如何通过 File.Exists 阶段的。
此外,您的查询字符串只定义了 fileNameToInsert 而不是整个路径。正如其他人所说,您似乎也在将数据库路径定义为 csv 文件。
这是全部代码吗?真的觉得你在这里错过了什么。
我正在尝试将 CSV 文件中的数据插入本地 Access 数据库中的 table,但 CSV 文件与我的数据库位于不同的位置。 insert 语句试图插入一个位于 Windows 临时文件夹中的 CSV 文件,但我一直收到一条错误消息,提示找不到该对象(CSV 文件)。不胜感激,谢谢!
以下是我收到的错误:
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.
下面是我的代码:
//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.");
}
我可以看到您定义了 csv 文件路径和临时文件路径,但我没有看到您将 csv 复制到临时路径的位置,所以不知道它是如何通过 File.Exists 阶段的。
此外,您的查询字符串只定义了 fileNameToInsert 而不是整个路径。正如其他人所说,您似乎也在将数据库路径定义为 csv 文件。
这是全部代码吗?真的觉得你在这里错过了什么。