现在的位置: 首页 > 综合 > 正文

使用VC6.0 连接PostgreSQL数据库

2013年10月10日 ⁄ 综合 ⁄ 共 1146字 ⁄ 字号 评论关闭

 我使用的版本是

VC6 + postgresql-8.4.8-1-windows-binaries 

使用这个版本的好处是解压就可以获得一些文件支持,

pgsql\include

pgsql\lib 

依赖文件在bin下有:

如果就是演示我下面的例子: 需要 

    comerr32.dll

    gssapi32.dll

    iconv.dll
    k5sprt32.dll
    krb5_32.dll
    libeay32.dll
    libiconv-2.dll
    libintl-8.dll
    libpq.dll
    libxml2.dll
    libxslt.dll
    msvcr71.dll
    ssleay32.dll
    zlib1.dll

解压后这2个目录有你要的文件(开发用的)

下面上代码吧

// pgDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "libpq-fe.h"

int main(int argc, char* argv[])
{
  PGconn * connection; 
  /** 顺带解释下,链接字符串的构成
  Table 8.2. Connection Attributes Connect-String Keyword
  Environment
Variable Example
  user
PGUSER user=korry
  password
PGPASSWORD password=cows
  dbname
PGDATABASE dbname=accounting 
  host
PGHOST host=jersey
  hostaddr
PGHOSTADDR hostaddr=127.0.0.1
  service
PGSERVICE service=accounting 
  port
PGPORT port=5432 
  **/
  
  // 用下面这个是因为不需要SSL的支持,加上IP就需要了
  // 注意大小写敏感
  connection = PQconnectdb( "dbname=mydb user=Eagle password=123456");  
       
  if ( PQstatus(connection) != CONNECTION_OK )
  {
  printf("%s \n", PQerrorMessage(connection) );
  }
  else
  {
  printf("connect is ok \n");
  }
  PQfinish( connection);
  
  return 0;
}

运行结果如下:

( "dbname=mydb user=Eagle password=123456");   使用这个参数的意: 自己的数据库名字: mydb 下面的应该不用解释了 地球人都知道

运行截图附上: 

抱歉!评论已关闭.