我有一个应用程序,需要连接到Postgres DB。我有我的连接函数,但每次尝试连接到DB时都会出现异常:
{System.Net.Sockets.SocketException (0x80004005):拒绝连接 at Npgsql.NpgsqlConnector.Connect(Npgsql.NpgsqlTimeout timeout)[0x001f5] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:733 at Npgsql.NpgsqlConnector + d__ 153.MoveNext()[0x005a0] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:648 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在 Npgsql.NpgsqlConnector + d__ 149.MoveNext()[0x003e1] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:455 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在 Npgsql.ConnectorPool + d__ 19.MoveNext()[0x001cf] in C:\ projects \ npgsql \ src \ Npgsql \ ConnectorPool.cs:300 ---在上一个位置引发异常的堆栈跟踪的结尾---在<1d288dd8ebaf4c6f890e1e99a5a184f0>:0中,System.Threading.Tasks.ValueTask`1 [TResult].get_Result()在 System.Runtime.CompilerServices.ValueTaskAwaiter`1[TResult].GetResult() [0x00000] 中获取结果 。 at Npgsql.NpgsqlConnection+<>c__DisplayClass32_0 +<g__OpenLong | 0> d.MoveNext()[0x0046d] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnection.cs:331 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在C:\ Users \ Mario \ Documents \ projetos \ App1 \ App1 \ App1 \ MainPage.xaml.cs:40中,Npgsql.NpgsqlConnection.Open()[0x00000]在App1.MainPage.conexao()中使用的代码如下:
异常发生在:
{System.Net.Sockets.SocketException (0x80004005):拒绝连接 at Npgsql.NpgsqlConnector.Connect(Npgsql.NpgsqlTimeout timeout)[0x001f5] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:733 at Npgsql.NpgsqlConnector + d__ 153.MoveNext()[0x005a0] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:648 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在 Npgsql.NpgsqlConnector + d__ 149.MoveNext()[0x003e1] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnector.cs:455 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在 Npgsql.ConnectorPool + d__ 19.MoveNext()[0x001cf] in C:\ projects \ npgsql \ src \ Npgsql \ ConnectorPool.cs:300 ---在上一个位置引发异常的堆栈跟踪的结尾---在<1d288dd8ebaf4c6f890e1e99a5a184f0>:0中,System.Threading.Tasks.ValueTask`1 [TResult].get_Result()在 System.Runtime.CompilerServices.ValueTaskAwaiter`1[TResult].GetResult() [0x00000] 中获取结果 。 at Npgsql.NpgsqlConnection+<>c__DisplayClass32_0 +<g__OpenLong | 0> d.MoveNext()[0x0046d] in C:\ projects \ npgsql \ src \ Npgsql \ NpgsqlConnection.cs:331 ---在上一个位置引发异常的堆栈跟踪的结尾--- 在C:\ Users \ Mario \ Documents \ projetos \ App1 \ App1 \ App1 \ MainPage.xaml.cs:40中,Npgsql.NpgsqlConnection.Open()[0x00000]在App1.MainPage.conexao()中使用的代码如下:
private void conexao()
{
NpgsqlConnection pgsqlConnection = null;
var connString = string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
serverName, port, userName, password, databaseName);
DataTable dt = new DataTable();
try
{
using (pgsqlConnection = new NpgsqlConnection(connString))
{
usuario usuario = new usuario();
// abre a conexão com o PgSQL e define a instrução SQL
pgsqlConnection.Open();
string cmdSeleciona = "Select * from funcionarios order by id_funcionario";
List<string> lista = new List<string>();
NpgsqlCommand cmd = new NpgsqlCommand(cmdSeleciona, pgsqlConnection);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
usuario.nome_funcionario = reader["nome_funcionario"].ToString();
usuario.email = reader["email"].ToString();
}
//using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(cmdSeleciona, pgsqlConnection))
//{
//}
}
}
catch (NpgsqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
pgsqlConnection.Close();
}
}
异常发生在:
pgsqlConnection.Open();
。