我正在使用asp.net,NetFramework 4.5,mssql开发网站。我通过nuget包管理器添加了npgsql.dll,用于连接到远程服务器中的postgresql数据库。 Everthing在我的本地机器上工作但在主机上不起作用。
public static string IsConnection()
{
try
{
NpgsqlConnection Conn = new NpgsqlConnection(Npgcstring);
//Conn.Open();
return "DB success";
}
catch (Exception ex)
{
return ex.Message + "DB Catch";
}
}
返回:“对象引用未设置为object.DB Catch的实例” 我尝试了NpgsqlException和SqlException而不是Exception,但它没有触发catch,它在页面中返回“对象引用没有设置为对象的实例”,我写道:
var Conn = new NpgsqlConnection(Npgcstring);
而不是:
NpgsqlConnection Conn = new NpgsqlConnection(Npgcstring);
返回相同的异常。
注意:只有Npgsql.dll的代码不起作用,其他代码在我收到的主机中有效
我们联系我们主办的公司。他们说“没有限制”。 我尝试了另一个服务器,提供免费托管,一切正常。 你遇到过这样的问题吗?
编辑:
catch (Exception ex)
{
return "DB Catch::" + FullException(ex);
}
public static string FullException(Exception exception)
{
var stringBuilder = new StringBuilder();
while (exception != null)
{
stringBuilder.AppendLine(exception.Message);
stringBuilder.AppendLine(exception.StackTrace);
exception = exception.InnerException;
}
return stringBuilder.ToString();
}
结果:
DB Catch::Object reference not set to an instance of an object.
at Npgsql.ConnectorPool..ctor(NpgsqlConnectionStringBuilder settings, String connString)
at Npgsql.NpgsqlConnection.GetPoolAndSettings()
at Npgsql.NpgsqlConnection.set_ConnectionString(String value)
at Npgsql.NpgsqlConnection..ctor(String connectionString)
at DAL.DB.IsConnection()