[英]after email deleting attachment file, error “The process cannot access the file because it is being used by another process.”
[英]c# The process cannot access the file because it is being used by another process. Even after closing the connection on sqlite 3
所以我试图根据我的网络数量创建多个 SQLite 文件。 它已成功创建 SQLite 文件,但是当我尝试将其设为 zip 文件时,它给了我一个无法访问该文件的异常,因为它正被另一个进程使用。
SqlConnection conn = new SqlConnection(cmn.connString);
conn.Open();
string query = "select networkid, network from custom_networkList";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int networkid = Convert.ToInt32(reader["networkid"]);
string network = reader["network"].ToString();
File.Copy(Templatefile, newfile + network + ".sqlite", true);
SQLiteConnection m_dbConnection = new SQLiteConnection(@"Data Source=" + newfile + network + ".sqlite;Version=3;");
m_dbConnection.Open();
SQLiteCommand command = new SQLiteCommand("begin", m_dbConnection);
command.ExecuteNonQuery();
insertZone(m_dbConnection);
InsertJunctions(m_dbConnection, networkid);
InsertHydrant(m_dbConnection, networkid);
insertWaterTank(m_dbConnection, networkid);
insertPump(m_dbConnection, networkid);
InsertReservoir(m_dbConnection, networkid);
insertValve(m_dbConnection, networkid);
insertPipe(m_dbConnection, networkid);
command = new SQLiteCommand("end", m_dbConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();
command.Dispose();
m_dbConnection.Dispose();
}
conn.Close();
GC.WaitForPendingFinalizers();
GC.Collect();
if (!Directory.Exists(newfilename))
{
// Try to create the directory.
File.Delete(newfilename);
}
ZipFile.CreateFromDirectory(newfile, newfilename, CompressionLevel.Fastest, true);
Directory.Delete(newfile,true);
return newfilename2;
尝试一下,我认为如果使用
using (SqlConnection conn = new SqlConnection(cmn.connString))
{
conn.Open();
string query = "select networkid, network from custom_networkList";
using (SqlCommand cmd = new SqlCommand(query, conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int networkid = Convert.ToInt32(reader["networkid"]);
string network = reader["network"].ToString();
File.Copy(Templatefile, newfile + network + ".sqlite", true);
using (SQLiteConnection m_dbConnection = new SQLiteConnection(@"Data Source=" + newfile + network + ".sqlite;Version=3;"))
{
m_dbConnection.Open();
using (SQLiteCommand command = new SQLiteCommand("begin", m_dbConnection))
{
command.ExecuteNonQuery();
insertZone(m_dbConnection);
InsertJunctions(m_dbConnection, networkid);
InsertHydrant(m_dbConnection, networkid);
insertWaterTank(m_dbConnection, networkid);
insertPump(m_dbConnection, networkid);
InsertReservoir(m_dbConnection, networkid);
insertValve(m_dbConnection, networkid);
insertPipe(m_dbConnection, networkid);
using (command = new SQLiteCommand("end", m_dbConnection))
{
command.ExecuteNonQuery();
}
// m_dbConnection.Close();
// command.Dispose();
}
}
}
}
}
GC.WaitForPendingFinalizers();
GC.Collect();
if (!Directory.Exists(newfilename))
{
// Try to create the directory.
File.Delete(newfilename);
}
ZipFile.CreateFromDirectory(newfile, newfilename, CompressionLevel.Fastest, true);
Directory.Delete(newfile, true);
return newfilename2;
要处理任何文件,您必须执行以下操作
using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
}
// or
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
}
// you can zip file or do what you want here
}
希望你能解决这个问题
在较新的 SQLite 客户端版本上,由于行为改变:连接池的引入,可能会发生错误“进程无法访问文件'...',因为它正被另一个进程使用”。 我有效地使用了以下解决方法,通过将 Pooling=false 添加到连接字符串来禁用池。
来自: https://github.com/dotnet/efcore/issues/27139#issuecomment-1007588298
SQLite 提供程序现在汇集连接,显着提高连接速度但保持连接打开。 您可以通过将 Pooling=false 添加到连接字符串或在您希望它们关闭的位置调用 SqliteConnection.ClearAllPools() 来禁用池。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.