2011-05-16 14 views
0

私はデータベースに接続するマルチスレッドアプリケーションを実行しています。接続が完了すると接続は閉じられます(すべてのスレッドを破棄します)。私はpooling = falseを設定して接続プールをクリアしようとしました。私は.disposeと.closeの両方を使用しました。vb.netでSQLConnection文字列を完全に閉じますか?

接続がすべて閉じられた後に接続先のデータベースを削除しようとすると、「現在データベースが使用中なのでデータベースを削除できません」というエラーが表示されます。以下は私のコードです

Dim comExecuteInsert As New SqlCommand 
Dim comm As New SqlConnection 
If (Not comm Is Nothing) Then 
    comm = Nothing 
End If 
comExecuteInsert.Connection = comm 
comExecuteInsert.CommandType = CommandType.StoredProcedure 
comExecuteInsert.CommandText = strProcedureName 
comExecuteInsert.CommandTimeout = 26000 
comExecuteInsert.Parameters.Add("@tableName", SqlDbType.VarChar, 100).Value = strTableName 
comExecuteInsert.Parameters.Add("@filename", SqlDbType.VarChar, 500).Value = strFileName 
comExecuteInsert.ExecuteScalar() 
comExecuteInsert.Parameters.Clear() 

comExecuteInsert = Nothing 
comm.Close() 
SqlConnection.ClearPool(comm) 

ストアドプロシージャは、後でデータベースに存在するテーブルにデータをドロップして挿入する一時テーブルを作成します。

+0

は、それはあなたがデータベースをドロップするか、ちょうどあなたが(実際には、接続プールに接続を返し、それ以降でのみ、実際にそれを閉じて)接続を閉じることを確認しようとしているということですか? – Thomas

+0

@mellamokb私は、コードの2行目から最後の行まで、comm.Close()との接続を閉じます。 – RobinReborn

+0

@Thomas接続を終了したいのでデータベースを削除します。 – RobinReborn

答えて

1

あなたはコマンドオブジェクトを廃棄していません。代わりに

comExecuteInsert = Nothing 

comExecuteInsert.Dispose 

または、次を試しをしてみてください。

Using comm As New SqlConnection 
    Using comExecuteInsert As New SqlCommand 
    comExecuteInsert.Connection = comm 
    comExecuteInsert.CommandType = CommandType.StoredProcedure 
    comExecuteInsert.CommandText = strProcedureName 
    comExecuteInsert.CommandTimeout = 26000 
    comExecuteInsert.Parameters.Add("@tableName", SqlDbType.VarChar, 100).Value = strTableName 
    comExecuteInsert.Parameters.Add("@filename", SqlDbType.VarChar, 500).Value = strFileName 
    comExecuteInsert.ExecuteScalar() 
    comExecuteInsert.Parameters.Clear() 
    comm.Close() 
    End Using 
End Using 
1

あなたがそれをドロップすることができますので、DBへの接続をクローズしようとしている場合、あなたはManagement Studioでmasterデータベースに対して、次のような何かを実行行うことができますDoes End Using close an open SQL Connection

+0

私はそれを試して、それは動作しません、おそらく私のデータベースを開いたままにする私のストアドプロシージャに何かがあります。 – RobinReborn

2

を参照してください、使用してキーワードを使用します。

コードでこれを行うには、masterデータベースとは別の接続を開き、上記の各ステートメントを個別に実行する必要があります(GOワードを実行しないでください)。これにより、何が行われているのかにかかわらず、自分以外のものを含めてデータベースに接続すると、すべてが殺されることに注意してください。

+0

これは私のために働いた唯一のものです! –

2

私はあなたがデータベースを落としたと述べビットを逃しました。あなたは使用する必要があります

SqlConnection.ClearAllPools() 

再利用のためのSQL Serverキャッシュ接続。上記は、キャッシュされた接続をクリアします。例えば

...

Sub Demo_DropDatabase(ByVal strCnn As String, ByVal strDBName As String) 
    Using cnnSQLS As New SqlConnection(strCnn) 
    SqlConnection.ClearAllPools() 
    cnnSQLS.Open() 
    Dim strSQL As String = "DROP DATABASE [" & strDBName & "]" 
    Using cmdDrop As New SqlCommand(strSQL, cnnSQLS) 
     cmdDrop.ExecuteNonQuery() 'N.B. may throw exception ' 
    End Using 
    cnnSQLS.Close() 
    End Using 
End Sub 
関連する問題