2014-06-09 106 views
0

我想從aspnet(c#)中刪除數據庫。但它給了我一個錯誤:有一些聯繫。 如果我刪除所有連接:Drop 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'; 

它給了我另一個錯誤:連接輸。

這就像是我執行上面的句子,它正在關閉我所有的連接。

我使用npgsql作爲連接器。

這裏我的代碼: 按鈕中的代碼:

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

它不適用於我。 而我不想阻止連接,我想突然刪除數據庫。 我編輯說,是的,它在postgresql客戶端(maestro),但不工作在C# – Za7pi

+0

向我們顯示的代碼不工作。編輯你的問題並粘貼它。 – Endrju

回答

0

*儘量使用psql來終止您的postgreSQL database

  • 爲您在使用C#創建一個batch file(*.bat), 此.bat文件必須包含以下腳本終止您 數據庫連接

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

例如

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 
  • 此代碼將在這條道路D:\Apps\RSTAR PGSQL DOTCONNECT\bin\Debug\PG

  • pgTerminate.bat看起來像

    CD/DD創建pgTerminate.bat文件:\ 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'; 

注意:您應該手動創建試圖代碼之前終止bat文件和測試無論其工作與否

+0

蝙蝠是好嗎? 這條線是在蝙蝠內部還是外部? 'cd/D D:\ Apps' – Za7pi

+2

@hector你的答案幫助了我! –

+0

@ Za7pi它裏面只有...定義'psql'的路徑在你的情況下,這將改變 –