【发布时间】:2018-10-05 12:19:24
【问题描述】:
我编写了一个 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 行触发。
【问题讨论】:
标签: c# windows-services