2014-06-09 3 views
0

J'essaie de supprimer une base de données de aspnet (C#). Mais ça me donne une erreur: il y a des connexions. Si je supprime toutes les connexions:Baisse de la base de données Postgresql

SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.pid <> pg_backend_pid() and pg_stat_activity.datname = 'databsename'; 

Il me donne une autre erreur: connexion est lose. C'est comme si j'exécutais la phrase ci-dessus, Il ferme toutes mes connexions.

J'utilise npgsql comme connecteur.

Voici mon code: Le code dans un bouton:

NpgsqlConnection _connPgComienzo = new NpgsqlConnection("my_connection_to_other_DDBB_in_the_same_server;"); 

    try 
    { 

     _connPgComienzo.Open(); 
     FileInfo file1 = new FileInfo(Server.MapPath("desconectar.sql")); 
     string script_crear_bbdd = file1.OpenText().ReadToEnd(); 
     var m_createdb_cmd1 = new NpgsqlCommand(script_crear_bbdd, _connPgComienzo); 
     m_createdb_cmd1.ExecuteNonQuery(); 
     _connPgComienzo.Close(); 

     _connPgComienzo.Open(); 
     FileInfo file2 = new FileInfo(Server.MapPath("drop_bbdd.sql")); 
     string script_crear_bbdd2 = file2.OpenText().ReadToEnd(); 
     var m_createdb_cmd2 = new NpgsqlCommand(script_crear_bbdd2, _connPgComienzo); 
     m_createdb_cmd2.ExecuteNonQuery(); 
     _connPgComienzo.Close(); 

    } 
    catch (Exception ex) 
    { 

    } 

desconectar.slq:

SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pid <> pg_backend_pid() AND pg_stat_activity.datname = 'theDDBBIWantToDrop'; 

drop_bbdd.sql:

DROP DATABASE theDDBBIWantToDrop; 
+0

http://stackoverflow.com/questions/17449420/postgresql-unable-to-drop-database-because -of-some-auto-connections-to-db? rq = 1 – Endrju

+0

Cela ne fonctionne pas pour moi. Et je ne veux pas empêcher la connexion, je veux laisser tomber la base de données soudainement. Je modifie pour dire que oui, il fonctionne dans un client postgresql (maestro), mais ne fonctionne pas en C# – Za7pi

+0

Montrez-nous le code qui ne fonctionne pas. Modifiez votre question et collez-la. – Endrju

Répondre

0

* Essayez d'utiliser psql à terminer votre postgreSQL database

  • pour que vous devez créer un batch file(*.bat) en utilisant C#, Ce fichier .bat doit contenir le script suivant pour mettre fin à votre connexion de base de données

psql -U postgres -d postgres -c SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname ='yourdb'

Par exemple

Private Sub TerminateDBactivity() 
    Dim strPG_dumpPath As String 
    Dim strSub As String 
    strPG_dumpPath = Application.StartupPath.ToString() 
    strPG_dumpPath = strPG_dumpPath.Replace("\", "\\") 
    Dim a As Integer = strPG_dumpPath.IndexOf(":\\", 0) 
    a = a + 2 
    strSub = strPG_dumpPath.Substring(0, (a - 2)) 
    strPG_dumpPath = strPG_dumpPath.Substring(a, (strPG_dumpPath.Length - a)) 
    Dim sbSB1 As New StringBuilder(strPG_dumpPath) 
    sbSB1 = sbSB1.Replace("\\", vbCrLf & vbCrLf & "cd ") 
    Dim sbSB2 As New StringBuilder("cd /D ") 
    sbSB2 = sbSB2.Append(strSub) 
    sbSB2 = sbSB2.Append(":\") 
    sbSB1 = sbSB2.Append(sbSB1) 
    sbSB1 = sbSB1.Append(vbCrLf & vbCrLf & "cd PG" & vbCrLf & vbCrLf) 
    Dim sw As New StreamWriter(Application.StartupPath.ToString() & "\PG\pgTerminate.bat") 
    If sbSB1.Length <> 0 Then 
     sbSB1 = sbSB1.Append("psql -U postgres -d postgres -c ""SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '" & gStrDBName & "';") 
     sw.WriteLine(sbSB1) 
     sw.Dispose() 
     sw.Close() 
     Dim processDB As New ProcessStartInfo 
     processDB.FileName = Application.StartupPath.ToString() & "\PG\pgTerminate.bat" 
     processDB.WindowStyle = ProcessWindowStyle.Hidden 
     Process.Start(processDB).WaitForExit() 
     Cursor.Current = Cursors.Default 
    End If 
End Sub 
  • ce code va créer un fichier pgTerminate.bat dans ce chemin D:\Apps\RSTAR PGSQL DOTCONNECT\bin\Debug\PG

  • pgTerminate.bat ressemble

    cd/DD: \ Apps

cd RSTAR PGSQL DOTCONNECT 

cd bin 

cd Debug 

cd PG 

psql -U postgres -d postgres -c SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'mydb'; 

Note: avant le code d'essayer, vous devez créer manuellement mettre fin à fichier de chauve-souris et le test que cela fonctionne ou non

+0

La chauve-souris est ok? Cette ligne est-elle à l'intérieur de la chauve-souris ou à l'extérieur? 'cd/D D: \ Apps' – Za7pi

+2

@hector votre réponse m'a aidé !! –

+0

@ Za7pi son intérieur seulement ... qui définit le chemin de 'psql' dans votre cas cela va changer –

Questions connexes