你好
我有一个程序检查文件夹中是否有Excel文件,然后通过SQL将其内容导入数据库表,然后将Excel文件移至“已完成”文件夹。
去年,此操作完美无缺。 Unfortunatley,我现在收到以下错误:
数据上传就可以了。这只是问题之后才移动文件。
以下是相关代码:
让我知道您是否需要更多信息。
非常感谢。
我有一个程序检查文件夹中是否有Excel文件,然后通过SQL将其内容导入数据库表,然后将Excel文件移至“已完成”文件夹。
去年,此操作完美无缺。 Unfortunatley,我现在收到以下错误:
System.IO.IOException:该进程无法访问该文件,因为它正在被另一个进程使用。
在System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath)
在System.IO .__ Error.WinIOError()
在System.IO.File.InternalMove(String sourceFileName,String destFileName,Boolean checkHost)
在System.IO.File.Move(字符串sourceFileName,字符串destFileName)
159:
数据上传就可以了。这只是问题之后才移动文件。
以下是相关代码:
C#:
private static void InsertExcelRecords(string filePath, string destinationPath)
{
LogWriter Log = new LogWriter("");
try
{
// ExcelConn(_path);
string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filePath);
OleDbConnection Econ = new OleDbConnection(constr);
string Query = string.Format("Select [Student ID],[Activity],[Session Name],[Date],[Time],[Status],[Sub-Session Name] FROM [{0}]", arg0: "data_RawAttendanceLog$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
//Create one dataset 和 fill this data set with this selected items, using oledbdataadpter
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
for (int i = Exceldt.Rows.Count - 1; i >= 0; i--)
{
if (Exceldt.Rows[i]["Student ID"] == DBNull.Value)
{
Exceldt.Rows[i].Delete();
}
}
Exceldt.AcceptChanges();
//creating object of SqlBulkCopy
string csDestination = "server = " + GetValueFromFile("server") + "; database = " + GetValueFromFile("database") + "; Trusted_Connection=True"; // User ID = *redacted*; Password = ; Trusted_Connection=True";
using (SqlConnection con = new SqlConnection(csDestination))
{
SqlBulkCopy objbulk = new SqlBulkCopy(con);
//assigning Destination table name
objbulk.DestinationTableName = GetValueFromFile("DestinationTableName");
//Mapping Table column
objbulk.ColumnMappings.Add("[Student ID]", "StudentID");
objbulk.ColumnMappings.Add("[Activity]", "Activity");
objbulk.ColumnMappings.Add("[Session Name]", "SessionName");
objbulk.ColumnMappings.Add("[Date]", "SessionDate");
objbulk.ColumnMappings.Add("[Time]", "SessionTime");
objbulk.ColumnMappings.Add("[Status]", "SessionStatus");
objbulk.ColumnMappings.Add("[Sub-Session Name]", "SubSessionName");
//inserting Datatable Records to DataBase
SqlConnection sqlConnection = new SqlConnection();
con.Open();
objbulk.WriteToServer(Exceldt);
Console.WriteLine("Upload successful.");
Log.LogWrite("Upload successful.");
Econ.Close();
Log.LogWrite("Moving " + filePath + " to " + destinationPath);
File.Move(filePath, destinationPath);
}
}
catch (Exception ex)
{
if (ex.Message.Contains("network-related"))
{
Console.WriteLine("Cancelled. No Network access.");
return;
}
else
{
Console.WriteLine(string.Format("Upload has not been imported due to: {0}", ex.Message));
Log.LogWrite(string.Format("Upload has not been imported due to: {0}", ex.Message));
Console.WriteLine("Press any key to close...");
ShowWindow(GetConsoleWindow(), SW_SHOW);
Console.ReadKey();
}
}
}
}
让我知道您是否需要更多信息。
非常感谢。