我编写了Windows服务,将CSV文件从原始位置复制到新位置。
此后,已读取新位置中的CSV并将数据写入MySQL。
由于文件移动服务会触发错误,因此上述任务曾经作为2个单独的服务运行:
{“该进程无法访问文件'C:\ data.csv',因为它是 被另一个进程使用。“}
因此,我决定将2个服务合并为1个,但是仍然遇到相同的问题。
我的代码如下。
program.cs
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
56 filemove(); <-- error trigger line
}
}
this.CloseConnection();
}
//文件移动功能
public void filemove()
{
string fileName = "data.csv";
string sourcePath = @"\\Data\Company Files\";
string targetPath = @"C:";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
//I won't include the whole code in this method since it's too lengthy.
}
Service.cs
我没有在方法中包含其余代码。
void timer1_Tick(object sender, ElapsedEventArgs e)
{
dbConnect.Truncate();
dbConnect.Insert();
dbConnect.filemove();
}
protected override void OnStart(string[] args)
{
dbConnect.Insert();
dbConnect.filemove();
}
该错误在Insert()方法内的第56行中触发。
答案 0 :(得分:0)
首先关闭您的连接,然后致电。
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
}
}
this.CloseConnection();
filemove();
}
答案 1 :(得分:0)
目前尚不清楚filemove()方法中发生了什么,以触发错误,这可能在未包含的部分进一步降低了。话虽如此,您可以尝试在StreamReader的using块之后移动对filemove()的调用(如下所示)。将它放在using块中,StreamReader仍然可以打开文件,并可能限制filemove()中发生的事情。
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value = (values[1]);
cmd.ExecuteNonQuery();
}
}
filemove(); // move this here after the using block
this.CloseConnection();
}
}
答案 2 :(得分:0)
打开文件进行读写时,需要确保已打开文件以与其他进程共享。为此,有一个名为FileShare的枚举! 试试这个:
using (var stream = new FileStream(@"C:\data.csv",FileMode.Open, FileAccess.Read,
FileShare.ReadWrite)
{
using(var reader = new StreamReader(stream))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value =
(values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
56 filemove(); <-- error trigger line
}
}
}