Until now I don't see any difference in using Npgsql driver instead of ODBC when working with PostgreSQL database on windows. But now I find one.
I have such code to check if table exists with ODBC:
Public Function dbTableExists(ByVal dbTable As String, ByVal dbName As String) As Boolean
Dim retval As Boolean = False
Dim nCon As New OdbcConnection
Dim btCommand As OdbcCommand = Nothing
nCon.ConnectionString = "Dsn=" + dbDsn + _
";database=" + dbName & _
";server=" + dbServer + _
";port=" + dbPort + _
";uid=" + dbUser + _
";pwd=" + dbPass
Try
nCon.Open()
btCommand = New OdbcCommand("SELECT 1 FROM pg_tables WHERE tablename='" & dbTable + "'", nCon)
retval = CBool(btCommand.ExecuteNonQuery())
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
retval = False
End Try
Return retval
End Function
With this code I get True if specific table exists in determined database or False otherwise.
When I try to use Npgsql instead of ODBC function is very similar:
Public Function tExists(ByVal dbTable As String, ByVal dbName As String) As Boolean
Dim retval As Boolean = False
Dim btCommand As NpgsqlCommand = Nothing
Dim nCon As New NpgsqlConnection(String.Format( _
"Server={0};Port={1};User Id={2};Password={3};Database={4};", _
dbServer, dbPort, dbUser, dbPass, dbName))
Try
nCon.Open()
btCommand = New NpgsqlCommand("SELECT 1 FROM pg_tables WHERE tablename='" & dbTable + "'", nCon)
retval = CBool(btCommand.ExecuteNonQuery())
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
retval = False
End Try
Return retval
End Function
But this won't work as expected.
I always get True as result no matter if specific table is present or not.
Any idea how to get Npgsql function to work?
executeNonQuery()? You are running a query, so shouldn't you be usingexecuteQuery()(or something similar - I don't know .Net)