简体   繁体   English

C# Npgsql 连接检查器异常抛出 System.NullReferenceException '对象引用未设置为 object 的实例。

[英]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.

相关问题 System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C# System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} C#未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 - C# Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException: '对象引用未设置为 object 的实例。 在处理通过 c# 编辑的 word 文档时 - System.NullReferenceException: 'Object reference not set to an instance of an object. while working with a word document to edit via c# 类型'System.NullReferenceException'的异常:对象引用未设置为对象的实例 - An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例。 抛出错误 - System.NullReferenceException: Object reference not set to an instance of an object. throwing error System.NullReferenceException:未将对象引用设置为对象的实例。 在测试自动化执行期间 - System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution System.NullReferenceException未将对象引用设置为对象的实例。 关于模型实例化 - 有时候 - System.NullReferenceException Object reference not set to an instance of an object. on Model instantiation - SOMETIMES “ System.NullReferenceException-对象引用未设置为对象的实例。” Web方法,用户控件 - “System.NullReferenceException— Object reference not set to an instance of an object.” Webmethod, User control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM