0

Im trying to move file after reading database file but im getting error that it is being used by another process.

My code

$sDatabasePath=testfile.dat
$sDatabaseConnectionString=[string]::Format("data source={0}",$sDatabasePath)
$oSQLiteDBConnection = New-Object System.Data.SQLite.SQLiteConnection
$oSQLiteDBConnection.ConnectionString = $sDatabaseConnectionString
$oSQLiteDBConnection.open()

$oSQLiteDBCommand=$oSQLiteDBConnection.CreateCommand()
$oSQLiteDBCommand.Commandtext="SELECT * from table_id"
$oSQLiteDBCommand.CommandType = [System.Data.CommandType]::Text
$oDBReader=$oSQLiteDBCommand.ExecuteReader()



while($oDBReader.HasRows) 
{
    if($oDBReader.Read())
    {
     $resultName = $oDBReader["name"]
    }
}




$oSQLiteDBConnection.close()


Move-Item -Path testfile.dat -Destination /somepath/


I found that it helped for others to add system gc collect but no luck for my code and still throws same error. I just need to close DB connection with releasing file and move file to another location

2
  • Put connection into a using block so resources are disposed after exiting block. See : stackoverflow.com/questions/42107851/… Commented Dec 13, 2022 at 13:16
  • 1
    Make sure to call the .Dispose() method for all .NET objects: $oSQLiteDBCommand, $oDBReader, $oSQLiteDBConnection | ForEach-Object Dispose Commented Dec 13, 2022 at 15:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.