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

标签 c# sqlite

所以我试图根据我的网络数量创建多个 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;

最佳答案

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;

要处理任何文件,您必须执行类似的操作

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
        }

希望你能解决这个问题

关于c# 该进程无法访问该文件,因为该文件正在被另一个进程使用。即使在 sqlite 3 上关闭连接后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503559/

相关文章:

c# - 从 .NET 中的字符串获取 URL 参数

c# - 扩展 Windows 键盘

sql - 存在一种从特定表中选择所有数据的简单方法

java - 创建连接时出现 SQLite NullPointerException 错误

Django - 从实时数据库中读取,由单独的应用程序创建

c# - 如何在 'await' 引发 EventHandler 事件

c# - 将命令绑定(bind)到动态创建的按钮

c# - 根据字符列表设置文本框的文本

java - Android 中的 SQLite OpenDatabase 问题

python - Sqlite 中的快速行数