我正在尝试从asp.net (c#)中删除数据库。但它给了我一个错误:有一些联系。如果我删除所有的连接:
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;
*尝试使用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.piddatname = ' 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() & "PGpgTerminate.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() & "PGpgTerminate.bat"
processDB.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(processDB).WaitForExit()
Cursor.Current = Cursors.Default
End If
End Sub
此代码将在此路径下创建
pgTerminate.bat
文件D:AppsRSTAR PGSQL DOTCONNECTbinDebugPG
pgTerminate.bat
look likecd/D D: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文件并测试它是否工作