1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/***************************************************************************
my_connect.c - description
-------------------
begin : Wed Sep 8 2001
copyright : (C) MySQL AB 1995-2002, www.mysql.com
author : venu ( venu@mysql.com )
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/***************************************************************************
* *
* This is a basic sample to demonstrate how to connect to MySQL server *
* using MySQL ODBC 3.51 driver *
* *
***************************************************************************/
#include "my_utility.h" /* MyODBC 3.51 sample utility header */
/********************************************************
* main routine *
*********************************************************/
int main(int argc, char *argv[])
{
SQLHENV henv;
SQLHDBC hdbc;
SQLCHAR server_name[30];
SQLRETURN rc;
SQLINTEGER narg;
/*
* show the usage string when the user asks for this
*/
printf("***********************************************\n");
printf("usage: my_connect [DSN] [UID] [PWD] \n");
printf("***********************************************\n");
/*
* if connection string supplied through arguments, overrite
* the default one..
*/
for(narg = 1; narg < argc; narg++)
{
if ( narg == 1 )
mydsn = argv[1];
else if ( narg == 2 )
myuid = argv[2];
else if ( narg == 3 )
mypwd = argv[3];
}
printf("\n allocating environment handle ...");
rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
myenv(henv, rc);
printf("success..\n setting the environment version ...");
rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
myenv(henv, rc);
printf("success..\n allocating the connection handle ...");
rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc);
myenv(henv, rc);
printf("success..\n connecting to server using DSN '%s'...",mydsn);
rc = SQLConnect(hdbc, mydsn, SQL_NTS, myuid, SQL_NTS, mypwd, SQL_NTS);
mycon(hdbc, rc);
printf("success..\n");
rc = SQLGetInfo(hdbc,SQL_DBMS_NAME,&server_name,40,NULL);
mycon(hdbc, rc);
printf(" connection established successfully to server '%s'\n",server_name);
rc = SQLDisconnect(hdbc);
mycon(hdbc, rc);
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
mycon(hdbc, rc);
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv);
myenv(henv, rc);
printf("\nSUCCESS ...\n\n");
return(0);
}
|