2017-11-27 12 views
0

私は自分のネットワークの数に基づいて複数のSQLiteファイルベースを作成しようとしていた。 これはSQLiteファイルを正常に作成しましたが、zipファイルにしようとすると別のプロセスで使用されているため、ファイルにアクセスできない例外が発生しました。c#別のプロセスで使用されているため、プロセスはファイルにアクセスできません。でも、sqlite 3の接続を閉じた後

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; 

答えて

1

あなたはこの

​​ ような何かを持っている任意のファイルを扱うため

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; 

を使用してを使用する場合、それは良くなる私の意見では、これを試してみてください

この問題を解決できることを願って

+0

私はすでに同じエラーが発生しています。最初は正常に実行され、エラーは発生しませんが、もう一度実行するとエラーが発生しています。別のファイルで処理されています – gray

+0

数分後に動作します – gray

関連する問題