简体   繁体   English

c# 进程无法访问该文件,因为它正被另一个进程使用。 即使在 sqlite 3 上关闭连接后

[英]c# The process cannot access the file because it is being used by another process. Even after closing the connection on sqlite 3

So i was trying to create multiple SQLite file base on the number of my network.所以我试图根据我的网络数量创建多个 SQLite 文件。 It has successfully created the SQLite files but when i tried to make it a zip file it gave me an exception that cannot access the file because it is being used by another process.它已成功创建 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;

Try this In my opinion It will be better if you use using 尝试一下,我认为如果使用

 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;

for handle any file you have to do something like this 要处理任何文件,您必须执行以下操作

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
        }

hope you can solve this problem 希望你能解决这个问题

On more recent SQLite client versions, the error "The process cannot access the file '...' because it is being used by another process" can happen due to a change of behaviour: the introduction of connection pool.在较新的 SQLite 客户端版本上,由于行为改变:连接池的引入,可能会发生错误“进程无法访问文件'...',因为它正被另一个进程使用”。 I have used effectively the following workaround to disable pooling by adding Pooling=false to the connection string.我有效地使用了以下解决方法,通过将 Pooling=false 添加到连接字符串来禁用池。

From: https://github.com/dotnet/efcore/issues/27139#issuecomment-1007588298来自: https://github.com/dotnet/efcore/issues/27139#issuecomment-1007588298

The SQLite provider now pools connections, improving connection speed significantly but keeping connections open. SQLite 提供程序现在汇集连接,显着提高连接速度但保持连接打开。 You can disable pooling by adding Pooling=false to the connection string or calling SqliteConnection.ClearAllPools() at the point where you want them to be closed.您可以通过将 Pooling=false 添加到连接字符串或在您希望它们关闭的位置调用 SqliteConnection.ClearAllPools() 来禁用池。

Also documented in https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#sqlite-connections-are-pooled还记录在https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/break-changes#sqlite-connections-are-pooled

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 email 删除附件文件后,错误“该进程无法访问该文件,因为它正在被另一个进程使用。” - after email deleting attachment file, error “The process cannot access the file because it is being used by another process.” 该进程无法访问文件“文件名”,因为它正在被另一个进程使用。 并且拒绝访问路径“文件名” - the process cannot access the file 'filename' because it is being used by another process. and Access to the path 'filename' is denied c#无法访问文件,因为它正被另一个进程使用 - c# Cannot access file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用。 文件复制 - The process cannot access the file because it is being used by another process. File.Copy System.IO.IOException: '进程无法访问文件“文件位置”,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file "file location"because it is being used by another process.' 该进程无法访问该文件,因为该文件正在被另一个进程使用。 File.Create方法 - The process cannot access the file because it is being used by another process. File.Create method 不断得到“因为它正在被另一个进程使用。” ` 即使我在导入 Excel 文件后关闭连接 - Keep getting `because it is being used by another process.' ` even after I close the connection after importing Excel file 为什么收到“该进程无法访问该文件,因为该文件正在被另一个进程使用。” - Why am i receiving a “The process cannot access the file because it is being used by another process.” 如何避免异常进程无法访问该文件,因为它正由另一个进程使用。 在.net - How to avoid exception The process cannot access the file because it is being used by another process. in .net “该进程无法访问该文件,因为它正被另一个进程使用。” 使用 SystemReader - “The process cannot access the file because it is being used by another process.” with SystemReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM