[英]C# Npgsql connection checker Exception Throw System.NullReferenceException 'Object reference not set to an instance of an object.'
My code try to check is the PostgreSQL server running and accessible by given credentials for given database.我的代码尝试检查 PostgreSQL 服务器是否正在运行,并且可以通过给定数据库的给定凭据进行访问。 Use the Npgsql NuGet.
使用 Npgsql NuGet。 If all the data, also IP, Port, User, Pass and Database are correct, the code returns true.
如果所有数据,IP、Port、User、Pass 和 Database 都正确,则代码返回 true。
Here is a code:这是一个代码:
public Boolean InsertInToPGSQL()
{
var pg_con_string = "Server=" + sqlinsertserver + ";" + "Port=" + sqlinsertport + ";" + "Database=" + sqlinsertdatabase + ";" + "User ID=" + sqlinsertusername + ";" + "Password=" + sqlinsertpassword + ";";
var pg_connetion = new NpgsqlConnection(pg_con_string);
try
{
if (pg_connetion != null)
{
pg_connetion.Open();
pg_connetion.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
The problem is if i write an IP address where is no server running, or bad port, or wrong user/pass, database, i receive always on:问题是如果我写一个 IP 地址,其中没有服务器在运行,或者端口错误,或者错误的用户/密码,数据库,我总是收到:
pg_connetion.Open();
An error:一个错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException:“对象引用未设置为 object 的实例。”
The problem is that a catch (Exception) don't catch this error and returns simple false.问题是一个catch(异常)没有捕捉到这个错误并返回简单的假。 I don't need to deal with type of errors, like wrong credential or IP / port, just need to have true on successful connection to the PostgreSQL server and false for any other case.
我不需要处理错误类型,例如错误的凭据或 IP / 端口,只需要在成功连接到 PostgreSQL 服务器时设置为 true,对于任何其他情况为 false。
How to deal with this error?如何处理这个错误?
To get exception details you need to do catch(Exception ex) and within catch block you can do ex.InnerException or ex.Message to find the exception details.要获取异常详细信息,您需要执行 catch(Exception ex) 并且在 catch 块中您可以执行 ex.InnerException 或 ex.Message 来查找异常详细信息。 In your case, the connection object is null because of incorrect parameters.
在您的情况下,由于参数不正确,连接 object 是 null。 I would do a null check on connection object before connection.open() which would avoid object ref not set to instance.
我会在 connection.open() 之前对连接 object 进行 null 检查,这将避免 object 引用未设置为实例。 Hope that should suffice your need.
希望这应该足以满足您的需要。 Change your code to following and send me the ex.Message and ex.InnerException values.
将您的代码更改为以下并将 ex.Message 和 ex.InnerException 值发送给我。
public static class Application
{
public static void Main()
{
if (InsertInToPGSQL())
Console.WriteLine("Connection successful");
else
Console.WriteLine("Connection failed");
Console.ReadLine();
}
public static Boolean InsertInToPGSQL()
{
var pg_con_string = "";
var pg_connetion = new NpgsqlConnection(pg_con_string);
try
{
if (pg_connetion != null)
{
pg_connetion.Open();
pg_connetion.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
}
`` ``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.