vba and postgresql connection - postgresql

I have a problem with connection from VisualBasic (Excel) to postgresql.
I define my connection like that:
Set ObjMyConn = New ADODB.Connection
ObjMyConn.ConnectionString = "Driver={PostgreSQL Unicode};Server=ip;Port=port;Database=db_name;Uid=user;Pwd=pass;"
ObjMyConn.Open
When I want to load 443532 rows into Excel with
Set objMyCmd = New ADODB.Command
Set objMyCmd.ActiveConnection = ObjMyConn
objMyCmd.CommandText = "select * from table"
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
It keeps showing me run-time error Out of memory error while reading tuples.
I have already upgraded my ODBC driver to the latest version. I read here that I have to set Use declare/catch to true somewhere (odbc driver I guess).
Is it possible to set it in VB code?

I believe you can just add the UseDeclareFetch=1option to the connection string like this:
"Driver={PostgreSQL Unicode};Server=ip;Port=port;Database=db_name;Uid=user;Pwd=pass;UseDeclareFetch=1"
But, if that doesn't work, there's another method - if you have set up a ODBC system data source with the ODBC Database Connection Administrator you can modify the registry key directly (provided you have the sufficient permissions).
The key you want to change is located under HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\PostgreSQL35Wif you're using a System DSN and the following vba code will enable Use Declare/Fetch:
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\ODBC\ODBC.INI\PostgreSQL35W"
strValueName = "UseDeclareFetch"
strStringValues = "1"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strStringValues

Related

Error 20599 Cannot open SQL Server using VB6/Crystal report

I have been trying to browse a SQL Server database-based Crystal Reports version 8 with following Visual Basic code:
CrystalReport1.ReportFileName = "C:\Report1.rpt"
CrystalReport1.Destination = crptToWindow
CrystalReport1.DiscardSavedData = True
CrystalReport1.WindowState = crptMaximized
MsgBox "Ok!", vbInformation
CrystalReport1.Action = 1
I am using the Crystal Reports control. The database of the report is ODBC datasource connecting to SQL Server. Without DiscardSavedData this works, but it displays old data. With DiscardSavedData, I get the error message:
Error 20599 Cannot open SQL Server.
How do I solve this problem?
i have solved my issus by adding the connection string CrystalReport1.Connect, i'd like to share my solution for all.
CrystalReport1.ReportFileName = "C:\Report1.rpt"
CrystalReport1.Destination = crptToWindow
CrystalReport1.DiscardSavedData = True
CrystalReport1.Connect ="Data Source=Localhost;UID=sa;PWD=****;DSQ=Dat BdName;"
CrystalReport1.WindowState = crptMaximized
CrystalReport1.Action = 1

Error while installing Moodle 29

I have place moodle files: C:\xampp\htdocs\moodle, E:\moodledata and in mysql created database "moodle".I m getting this error message on the webpage when opening moodle
"Config table does not contain version, can not continue, sorry.
More information about this error
It is usually not possible to recover from errors triggered during installation, you may need to create a new database or use a different database prefix if you want to retry the installation."
Setting in config.php
<?php // Moodle configuration file
unset($CFG);
global $CFG;
$CFG = new stdClass();
$CFG->dbtype = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'moodle';
$CFG->dbuser = 'root';
$CFG->dbpass = '';
$CFG->prefix = 'lms_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
);
$CFG->wwwroot = 'http://'.$_SERVER['HTTP_HOST'].'/moodle';
$CFG->dataroot = 'E:\\moodledata';
$CFG->admin = 'admin';
$CFG->directorypermissions = 0777;
require_once(dirname(__FILE__) . '/lib/setup.php');
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!
It looks like it got interrupted during install and now the database is incomplete. You can try two things: insert the missing version number into the config table, for example by using phpMyAdmin, or delete the database, create a new one and try to install again.
To insert the version number:
open the file version.php to find the version number.
in your database manager, go to the table mdl_config
insert a new record with the values: name='version' and value='the version number you looked up in the version.php file'
this can happen if the install process is not perfect.
You haven't told us the version or anything about the 'local server'
so we can not see the specifics of your problem.
check the release notes for the version you are using and make sure 100% that your server meets the requirements.

Powershell Script to select records in PostgreSQL database [duplicate]

I'm trying to connect to a remote PostgreSql database using powershell. This is my first time using powershell so I'm sorry if this is a noob question. This is my Code:
$DBConnectionString = "Driver={PostgreSQL UNICODE}:Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM mytable;";
$DBCmd.ExecuteReader();
$DBConn.Close();
When I run this I get "Exception Calling "Open" with "0" argument(s): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". I've downloaded and installed the pgsqlodbc driver but I'm still getting this error. Does anyone have any ideas how I could fix this? I have searched the internet and I'm really not getting anywhere at this point.
Thanks.
Consult: https://odbc.postgresql.org/
Download: https://www.postgresql.org/ftp/odbc/versions/msi/
Data sources (ODBC) on Windows: Start → Search → odbc → User DSN → Add/Configure
Example :
$MyServer = "<ip>"
$MyPort = "5432"
$MyDB = "<database>"
$MyUid = "<user>"
$MyPass = "<pass>"
$DBConnectionString = "Driver={PostgreSQL UNICODE(x64)};Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM tb_module;";
$DBCmd.ExecuteReader();
$DBConn.Close();
You can use psql which comes with postgresql if you happen to have postgresql installed on your client
$dburl="postgresql://exusername:expw#exhostname:5432/postgres"
$data="select * from extable" | psql --csv $dburl | ConvertFrom-Csv
You must have psql in your path or reference it, its within e.g. C:\Program Files\PostgreSQL\12\bin. Should be able to type "psql" and see output within powershell.
Check if the DSN exists in ODBC data source. If not you have to create one going to 'Control Panel', 'Admin. Tools', 'Data Sources (ODBC)'. Then select 'Add User DSN'-
Select the PostgreSQL driver, and fill in your server and database details.
Test connection to check is all ok!
You actually have a typo in your connection string after Driver declaration.
There is a double colon instead of a semicolon :)
Wrong:
{PostgreSQL UNICODE} : Server
Correct:
{PostgreSQL UNICODE} ; Server
I found the problem, I thought the Postgresql ODBC driver was installed, but it wasn't. I finally got it to work after finding this site: http://code.google.com/p/visionmap/wiki/psqlODBC Then I followed the instructions above. it works.
Thanks for all the help.

WinSCP and defined port

I am trying to connect to our ftp using WinSCP. But how can i define a port in PowerShell using the .net assembly!
I am trying to make a solution where I download the recent files from the server, delete it on the server and then import it to a MSSQL Database.
But my issue now is connecting to the ftp using WinSCP.
It's hard to say without any code but try something like this:
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.PortNumber = 2222
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
WinSCP SessionOptions LINK
WinSCP Session Options default to SFTP and PORT 22 so a normal session object just needs a few things like this...
Note: I'm going to display in Visual Basic 6 for anyone having trouble using the library in this manner, but the logic is similar for VB.net, C#, and PowerShell as the question asks.
Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")
With sessionOptions
.HostName = SftpUrl
.UserName = UserName
.Password = UserPassword
.SshHostKeyFingerprint = "ssh-rsa 2048 Ue9.................................="
End With
Dim sftpSession
Set sftpSession = CreateObject("WinSCP.Session")
On Error GoTo YOURERROR
sftpSession.open sessionOptions
If sftpSession.opened Then
'Do stuff...
End If
The above code is working and connecting to a real server.
In your question, you originally asked for FTP, although you did correct and say SFTP. However, I'll also display a FTP request too as WinSCP supports it and the example WOULD need to set at least the protocol.
WinSCP sets the port based on the protocol used, so in the below example we still do not need to set the port.
One would only need to set the port if it differed from default server ports for the protocol used.
Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Ftp
.HostName = FtpUrl
.UserName = UserName
.Password = UserPassword
End With
Dim FtpSession
Set FtpSession = CreateObject("WinSCP.Session")
On Error GoTo YOURERROR
FtpSession.open sessionOptions
If FtpSession.opened Then
'Do stuff...
End If

Connect to remote PostgreSql database using Powershell

I'm trying to connect to a remote PostgreSql database using powershell. This is my first time using powershell so I'm sorry if this is a noob question. This is my Code:
$DBConnectionString = "Driver={PostgreSQL UNICODE}:Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM mytable;";
$DBCmd.ExecuteReader();
$DBConn.Close();
When I run this I get "Exception Calling "Open" with "0" argument(s): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". I've downloaded and installed the pgsqlodbc driver but I'm still getting this error. Does anyone have any ideas how I could fix this? I have searched the internet and I'm really not getting anywhere at this point.
Thanks.
Consult: https://odbc.postgresql.org/
Download: https://www.postgresql.org/ftp/odbc/versions/msi/
Data sources (ODBC) on Windows: Start → Search → odbc → User DSN → Add/Configure
Example :
$MyServer = "<ip>"
$MyPort = "5432"
$MyDB = "<database>"
$MyUid = "<user>"
$MyPass = "<pass>"
$DBConnectionString = "Driver={PostgreSQL UNICODE(x64)};Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM tb_module;";
$DBCmd.ExecuteReader();
$DBConn.Close();
You can use psql which comes with postgresql if you happen to have postgresql installed on your client
$dburl="postgresql://exusername:expw#exhostname:5432/postgres"
$data="select * from extable" | psql --csv $dburl | ConvertFrom-Csv
You must have psql in your path or reference it, its within e.g. C:\Program Files\PostgreSQL\12\bin. Should be able to type "psql" and see output within powershell.
Check if the DSN exists in ODBC data source. If not you have to create one going to 'Control Panel', 'Admin. Tools', 'Data Sources (ODBC)'. Then select 'Add User DSN'-
Select the PostgreSQL driver, and fill in your server and database details.
Test connection to check is all ok!
You actually have a typo in your connection string after Driver declaration.
There is a double colon instead of a semicolon :)
Wrong:
{PostgreSQL UNICODE} : Server
Correct:
{PostgreSQL UNICODE} ; Server
I found the problem, I thought the Postgresql ODBC driver was installed, but it wasn't. I finally got it to work after finding this site: http://code.google.com/p/visionmap/wiki/psqlODBC Then I followed the instructions above. it works.
Thanks for all the help.