我正在尝试从 aspnet(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)
{
}
deconectar.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;