static SqlServer_T doConnect(URL_T url, char **error) { RETCODE retcode; SqlServer_T db = (SqlServer_T)malloc(sizeof(struct SqlServer_S)); /*retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &db->henv); retcode = SQLSetEnvAttr(db->henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER); */ retcode = SQLAllocEnv(&db->henv); //2.Á¬½Ó¾ä±ú retcode = SQLAllocConnect(db->henv, &db->hdbc); //retcode = SQLAllocHandle(SQL_HANDLE_DBC, db->henv, &db->hdbc); retcode = SQLConnect(db->hdbc, URL_getParameter(url, "db"), strlen( URL_getParameter(url, "db") ), URL_getUser(url), strlen(URL_getUser(url)), URL_getPassword(url), strlen(URL_getPassword(url))); if (SQLSERVERSUCCESS(retcode)) { return db; } else { SQLFreeHandle(SQL_HANDLE_DBC, db->hdbc); SQLFreeHandle(SQL_HANDLE_ENV, db->henv); free(db); return NULL; } }
// Allocate environment handle, allocate connection handle, // connect to data source, and allocate statement handle. bool direxec::sqlconn(FILE* file) { logFile = file; unsigned int timeout = 10; // seconds SQLAllocEnv(&henv); SQLAllocConnect(henv,&hdbc); SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, &timeout, 0); rc=SQLConnect(hdbc,chr_ds_name,SQL_NTS,NULL,0,NULL,0); // Deallocate handles, display error message, and exit. if (!MYSQLSUCCESS(rc)) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLSMALLINT MsgLen; SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, SqlState, &NativeError, (unsigned char *) szData, 2048, &MsgLen); SQLFreeEnv(henv); SQLFreeConnect(hdbc); fprintf(logFile, "! Error while connecting to database: %s\n", szData); errorstate = TRUE; return FALSE; } rc=SQLAllocStmt(hdbc,&hstmt); errorstate = FALSE; return TRUE; }
static void gsql_connect (char *dsn, Database *db) { RETCODE status; db->connected = 0; if (dsn != (char *) NULL) { char buf[257]; short buflen; if (strchr (dsn, '=') == (char *)NULL) { db->dsn = (char *)xmalloc (5 + strlen (dsn)); sprintf (db->dsn, "DSN=%s", dsn); } else db->dsn = strdup (dsn); if (SQLAllocEnv (&(db->henv)) == SQL_SUCCESS) if (SQLAllocConnect (db->henv, &(db->hdbc)) == SQL_SUCCESS) { status = SQLDriverConnect (db->hdbc, 0, (UCHAR *) db->dsn, SQL_NTS, (UCHAR *) buf, sizeof (buf), &buflen, SQL_DRIVER_COMPLETE); if ((status == SQL_SUCCESS) || (status == SQL_SUCCESS_WITH_INFO)) db->connected = 1; } } }
int cezdb::Connect( char *dsn, int mode ) { RETCODE res; if ( flag != CEZDB_MODE_ENVREADY ) return -1; res = SQLAllocConnect( henv, &hdbc ); if ( res != SQL_SUCCESS ) { flag = CEZDB_MODE_ERROR; return -2; } switch( mode ) { case 1: res = SQLDriverConnect( hdbc, 0, (unsigned char *)dsn, SQL_NTS, StringOut, CEZDB_OUTBUFSIZE, NULL, SQL_DRIVER_COMPLETE_REQUIRED ); break; // case 2: // res = SQLBrowseConnect( hdbc,(unsigned char *)dsn, SQL_NTS,StringOut, CEZDB_OUTBUFSIZE, &len ); default: res = SQLDriverConnect( hdbc, 0, (unsigned char *)dsn, SQL_NTS, StringOut, CEZDB_OUTBUFSIZE, NULL, SQL_DRIVER_NOPROMPT ); break; } if ( res != SQL_SUCCESS ) { SQLFreeConnect( hdbc ); flag = CEZDB_MODE_ERROR; return -3; } //MessageBox( NULL, (char *)StringOut, "WOW", 0 ); // DBC Ready // flag = CEZDB_MODE_DBCREADY; return 0; }
static SQLRETURN init(char *dbalias, SQLHENV *henv, SQLHDBC *hdbc) { SQLHSTMT hstmt; /* statement handle */ SQLRETURN rc; *henv = 0; rc = SQLAllocEnv(henv); CHECK_HANDLE(SQL_HANDLE_ENV, *henv, rc, "SQLAllocEnv", (SQLCHAR *) ""); rc = SQLAllocConnect(*henv, hdbc); CHECK_HANDLE(SQL_HANDLE_DBC, *hdbc, rc, "SQLAllocConnect", (SQLCHAR *) ""); rc = SQLConnect(*hdbc, (SQLCHAR *) dbalias, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS); CHECK_HANDLE(SQL_HANDLE_DBC, *hdbc, rc, "SQLConnect", (SQLCHAR *) ""); rc = SQLAllocHandle(SQL_HANDLE_STMT, *hdbc, &hstmt); CHECK_HANDLE(SQL_HANDLE_STMT, hstmt, rc, "SQLAllocHandle", (SQLCHAR *) ""); (void) SQLExecDirect(hstmt, CTSQL, SQL_NTS); /*CHECK_HANDLE(SQL_HANDLE_STMT, hstmt, rc, "SQLExecDirect", CTSQL);*/ (void) SQLFreeStmt(hstmt, SQL_DROP); return rc; }
static BOOL test_driver_connect (TDSNCHOOSER *choose_t, char *connstr) { HENV henv; HDBC hdbc; #if (ODBCVER < 0x300) if (SQLAllocEnv (&henv) != SQL_SUCCESS) #else if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv) != SQL_SUCCESS) #endif { _iodbcdm_nativeerrorbox (choose_t->mainwnd, henv, SQL_NULL_HDBC, SQL_NULL_HSTMT); return FALSE; } #if (ODBCVER < 0x300) if (SQLAllocConnect (henv, &hdbc) != SQL_SUCCESS) #else SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); if (SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS) #endif { _iodbcdm_nativeerrorbox (choose_t->mainwnd, henv, hdbc, SQL_NULL_HSTMT); SQLFreeEnv (henv); return FALSE; } SQLSetConfigMode (ODBC_BOTH_DSN); if (SQLDriverConnect (hdbc, choose_t->mainwnd, connstr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_PROMPT) != SQL_SUCCESS) { _iodbcdm_nativeerrorbox (choose_t->mainwnd, henv, hdbc, SQL_NULL_HSTMT); SQLFreeEnv (henv); return FALSE; } else { SQLDisconnect (hdbc); } #if (ODBCVER < 0x300) SQLFreeConnect (hdbc); SQLFreeEnv (henv); #else SQLFreeHandle (SQL_HANDLE_DBC, hdbc); SQLFreeHandle (SQL_HANDLE_ENV, henv); #endif return TRUE; }
TF EXPFUNC p_db_connect(ENGid eid) { HDBC hdbc; char szDataSource[80]; char szUserID[40]; char szPassword[40]; if (pVAR != lsGetParmType(eid, 1)) { lsErrRaise(eid, "db_connect instantiation error: arg 1 must be var"); return FALSE; } g_lsRC = lsGetParm(eid, 2, cSTR, szDataSource); if (g_lsRC != OK) goto LS_ERROR; g_lsRC = lsGetParm(eid, 3, cSTR, szUserID); if (g_lsRC != OK) goto LS_ERROR; g_lsRC = lsGetParm(eid, 4, cSTR, szPassword); if (g_lsRC != OK) goto LS_ERROR; g_RC = SQLAllocConnect(g_hEnv, &hdbc); /* set up error handler */ g_hDBC = hdbc; g_hSTMT = SQL_NULL_HSTMT; g_eid = eid; ERROR_CHK("SQLAllocConnect"); // Added this line to get around erroneous messages: // S1010:[Microsoft][ODBC SQL Server Driver]Function sequence error // Fix from MS Knowledge Article 179226 // SQL_PRESERVE_CURSORS - note new name in actual call // This only applies to SQL Server, so hopefully, nothing bad will // happen if it fails. g_RC = SQLSetConnectOption(hdbc, SQL_COPT_SS_PRESERVE_CURSORS, NULL); g_RC = SQLConnect(hdbc, (UCHAR*)szDataSource, SQL_NTS, (UCHAR*)szUserID, SQL_NTS, (UCHAR*)szPassword, SQL_NTS); ERROR_CHK("SQLConnect"); lsUnifyParm(eid, 1, cADDR, &hdbc); return TRUE; LS_ERROR: return(lserror()); ODBC_ERROR: return FALSE; }
SQLRETURN alloc_handle(SQLHENV &env, SQLHDBC &dbc) { /* allocate Environment handle */ if (SQLAllocEnv(&env) != SQL_SUCCESS) { printf("SQLAllocEnv error!!\n"); return SQL_ERROR; } /* allocate Connection handle */ if (SQLAllocConnect(env, &dbc) != SQL_SUCCESS) { printf("SQLAllocConnect error!!\n"); return SQL_ERROR; } return SQL_SUCCESS; }
static int sql_odbc_init(const char *host, const char *port, const char *socket, const char *name, const char *user, const char *pass) { /* Create environment. */ if(odbc_Result(SQLAllocEnv(&env)) && odbc_Result(SQLAllocConnect(env, &con))) { eventlog(eventlog_level_debug, __FUNCTION__, "Created ODBC environment."); } else { eventlog(eventlog_level_error, __FUNCTION__, "Unable to allocate ODBC environment."); return odbc_Fail(); } if(odbc_Result(SQLConnect(con, (SQLCHAR*)name, SQL_NTS, (SQLCHAR*)user, SQL_NTS, (SQLCHAR*)pass, SQL_NTS))) { eventlog(eventlog_level_debug, __FUNCTION__, "Connected to ODBC datasource \"%s\".", name); } else { eventlog(eventlog_level_error, __FUNCTION__, "Unable to connect to ODBC datasource \"%s\".", name); odbc_Error(SQL_HANDLE_DBC, con, eventlog_level_error, __FUNCTION__); return odbc_Fail(); } return 0; }
/************************************************************************* * * Function: sql_socket_init * * Purpose: Establish connection to the db * *************************************************************************/ static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config) { rlm_sql_iodbc_conn_t *conn; SQLRETURN rcode; MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_iodbc_conn_t)); talloc_set_destructor((void *) conn, sql_socket_destructor); rcode = SQLAllocEnv(&conn->env_handle); if (!SQL_SUCCEEDED(rcode)) { ERROR("sql_create_socket: SQLAllocEnv failed: %s", sql_error(handle, config)); return -1; } rcode = SQLAllocConnect(conn->env_handle, &conn->dbc_handle); if (!SQL_SUCCEEDED(rcode)) { ERROR("sql_create_socket: SQLAllocConnect failed: %s", sql_error(handle, config)); return -1; } /* * The iodbc API doesn't qualify arguments as const even when they should be. */ { SQLCHAR *server, *login, *password; memcpy(&server, &config->sql_server, sizeof(server)); memcpy(&login, &config->sql_login, sizeof(login)); memcpy(&password, &config->sql_password, sizeof(password)); rcode = SQLConnect(conn->dbc_handle, server, SQL_NTS, login, SQL_NTS, password, SQL_NTS); } if (!SQL_SUCCEEDED(rcode)) { ERROR("sql_create_socket: SQLConnectfailed: %s", sql_error(handle, config)); return -1; } return 0; }
CDBConnection::CDBConnection() { RETCODE rc; hdbc = SQL_NULL_HDBC; rc = SQLAllocConnect (henv, &hdbc); if (rc != SQL_SUCCESS) { ReportODBCError (SQL_NULL_HSTMT, "SQLAllocConnect error"); throw rc; } rc = SQLConnect (hdbc, (SQLCHAR *)szDSN, SQL_NTS, (SQLCHAR *)szUID, SQL_NTS, (SQLCHAR *)szPWD, SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { ReportODBCError (SQL_NULL_HSTMT, "SQLConnect error"); SQLFreeConnect (hdbc); hdbc = SQL_NULL_HDBC; throw rc; } }
int odbc_createconn(ClipMachine * cm) { int dbl,userl,passwdl; char* db = _clip_parcl(cm,2,&dbl); char* user = _clip_parcl(cm,3,&userl); char* passwd = _clip_parcl(cm,4,&passwdl); ODBC_CONN *conn = NULL; SQLHDBC tmpconn; SQLRETURN er; SQLHENV henv; char dbms[30]; if(!user) userl = 0; if(!passwd) passwdl = 0; if((er = SQLAllocEnv(&henv))) goto err; if((er = SQLAllocConnect(henv,&tmpconn))) goto err; if((er = SQLConnect(tmpconn,db,dbl,user,userl,passwd,passwdl))) goto err; /* silly PostgreSQL's ODBC driver */ if((er = SQLGetInfo(tmpconn,SQL_DBMS_NAME,dbms,sizeof(dbms),0))) goto err; conn = calloc(sizeof(ODBC_CONN),1); conn->conn = tmpconn; conn->vtbl = &vtbl; conn->henv = henv; if(strcmp(dbms,"PostgreSQL")==0) conn->postgres = 1; return _clip_store_c_item(cm, (void *) conn, _C_ITEM_TYPE_SQL, destroy_odbc_conn); err: odbc_error(cm,(conn?conn->loc:0),henv,tmpconn,0,__LINE__,er_connect); if(conn) free(conn); return -1; }
int main (int argc, char *argv[]) { rc = SQLAllocEnv (&henv); IF_EERR_EXIT (henv, rc); rc = SQLAllocConnect (henv, &hdbc); IF_CERR_EXIT (hdbc, rc); rc = SQLConnect (hdbc, argc > 1 ? argv[1] : "1111", SQL_NTS, argc > 2 ? argv[2] : "dba", SQL_NTS, argc > 3 ? argv[3] : "dba", SQL_NTS); IF_CERR_EXIT (hdbc, rc); rc = SQLAllocStmt (hdbc, &hstmt); IF_CERR_EXIT (hdbc, rc); rc = SQLSetStmtOption (hstmt, SQL_ROWSET_SIZE, 10); rc = SQLSetStmtOption (hstmt, SQL_CURSOR_TYPE, SQL_CURSOR_KEYSET_DRIVEN); rc = SQLExecDirect (hstmt, "select distinct name_part (KEY_TABLE, 0) as DB from DB.DBA.SYS_KEYS", SQL_NTS); while (SQL_SUCCESS == (rc = SQLFetch (hstmt))) { char data[128]; SDWORD len = sizeof (data); IF_ERR_EXIT (hstmt, rc); rc = SQLGetData (hstmt, 1, SQL_C_CHAR, data, sizeof (data), &len); IF_ERR_EXIT (hstmt, rc); if (rc == SQL_SUCCESS) fprintf (stdout, "%s\n", data); } IF_ERR_EXIT (hstmt, rc); SQLDisconnect (hdbc); SQLFreeConnect (hdbc); SQLFreeEnv (henv); return 0; }
/*Function to connect a DB using ODBC*/ void connectDB(HENV *henv,HDBC *hdbc){ SSchar ODBC_database_name[80]; SSchar ODBC_database_user_id[80]; SSchar ODBC_database_passwd[80]; SQLRETURN return_code; return_code = SQLAllocEnv ((SQLHENV *)henv); if ((return_code == SQL_SUCCESS) || (return_code == SQL_SUCCESS_WITH_INFO)) return_code = SQLAllocConnect ((SQLHENV)*henv, (SQLHDBC *)hdbc); printf("This program attempts to connect to an ODBC database.\n"); printf("Enter the ODBC database name: "); SSgets (ODBC_database_name); if (ODBC_database_name[0] == NULL_CHAR) { printf("ODBC_database_name can't be null\n"); exit(1); } printf("Enter the user ID: "); SSgets(ODBC_database_user_id); printf("Enter the DB password: "******"Successful connection to database\n"); } else{ printf("Connection to database failed.\n"); ODBC_error (NULL, (HDBC)hdbc, NULL) ; exit(1); } }
int CSQLDirect::Connect( LPCSTR svSource , LPCSTR svUser, LPCSTR svPassword) { int nConnect=SQLAllocEnv( &m_hEnv ); if( nConnect==SQL_SUCCESS ) { nConnect=SQLAllocConnect( m_hEnv,&m_hDBC ); if( nConnect==SQL_SUCCESS ) { SQLSetConnectOption( m_hDBC,SQL_LOGIN_TIMEOUT,5 ); // Connect to the datasource. _TUCHAR *psvSource=new _TUCHAR[ strlen( svSource )+1 ]; _TUCHAR *psvUser=new _TUCHAR[ strlen( svUser )+1 ]; _TUCHAR *psvPassword=new _TUCHAR[ strlen( svPassword )+1 ]; strcpy( (char*)psvSource,svSource ); strcpy( (char*)psvUser,svUser ); strcpy( (char*)psvPassword,svPassword ); nConnect=SQLConnect( m_hDBC,psvSource,SQL_NTS,psvUser,SQL_NTS,psvPassword,SQL_NTS ); if( nConnect==SQL_SUCCESS || nConnect==SQL_SUCCESS_WITH_INFO ) { // Now that the datasource is open, get our SQL statement handle. nConnect=SQLAllocStmt( m_hDBC,&m_hStmt ); } SAFE_DELETE(psvSource);///modified by xizheng SAFE_DELETE(psvUser);///modified by xizheng SAFE_DELETE(psvPassword);///modified by xizheng } else DisplayError(); } else DisplayError(); return nConnect; }
BOOL CODBC::Connect(char* DSN, int timeout) { SQLAllocEnv (&henv); SQLAllocConnect (henv,&hdbc); SQLSetConnectAttr (hdbc, SQL_LOGIN_TIMEOUT, &timeout, 0); rc = SQLConnect (hdbc, (unsigned char*)DSN, SQL_NTS, NULL, 0, NULL, 0); // On error: deallocate handles, display error message, and exit. if (!MYSQLSUCCESS(rc)) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLSMALLINT MsgLen; SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, SqlState, &NativeError, (unsigned char *)lastError, 2048, &MsgLen); SQLFreeEnv(henv); SQLFreeConnect(hdbc); return FALSE; } // connecting was succesfull rc=SQLAllocStmt(hdbc, &hstmt); return TRUE; }
// ------------------------------------------------------------------------------------- SQLRETURN SQL_API SQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, _Out_ SQLHANDLE *OutputHandle) { AQ_ODBC_LOG("%s called\n", __FUNCTION__); switch ( HandleType ) { case SQL_HANDLE_ENV: return SQLAllocEnv ( OutputHandle ); case SQL_HANDLE_DBC: return SQLAllocConnect ( InputHandle, OutputHandle ); case SQL_HANDLE_STMT: return SQLAllocStmt ( InputHandle, OutputHandle ); case SQL_HANDLE_DESC: AQ_ODBC_LOG("SQLAllocHandle - Explicit descriptor requested - not supported\n"); return SQL_SUCCESS; } return SQL_ERROR; }
int main() { int i; retcode = SQLAllocEnv(&henv); if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(henv, SQL_NULL_HDBC, SQL_NULL_HSTMT, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLAllocConnect\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } retcode = SQLSetConnectOption(hdbc, SQL_ACCESS_MODE, SQL_MODE_READ_ONLY); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLSetConnectOption\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } retcode = SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLSetConnectOption\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } retcode = SQLConnect(hdbc, (UCHAR *)"Northwind", SQL_NTS, (UCHAR *)"", SQL_NTS, (UCHAR *)"", SQL_NTS); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLConnect\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } if (SQLAllocStmt(hdbc, &hstmt)!= SQL_SUCCESS) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLAllocStmt\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } /* Prepare the SQL statement with parameter markers. */ retcode = SQLPrepare(hstmt, (unsigned char *)"select * from Shippers", SQL_NTS); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { long p1; long p1Len = sizeof(p1); long sAge = 1023; long cbAge = sizeof(long); UCHAR szCol1[60]; SQLINTEGER length; printf("excecuting first statement\n"); retcode = SQLExecute(hstmt); if (retcode != SQL_SUCCESS) { UCHAR szSqlState[6]; UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER dwNativeError; SWORD wErrorMsg; SQLError(SQL_NULL_HENV, SQL_NULL_HDBC, hstmt, szSqlState, &dwNativeError, szErrorMsg, SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg); fprintf(stderr,"problem with SQLExecute\n%s\n%s\n", szSqlState, szErrorMsg); return 1; } SQLBindCol(hstmt, 3, SQL_CHAR, szCol1, 60, &length); //SQLBindCol(hstmt, 1, SQL_CHAR, szCol1, 60, NULL); /* Execute statement with first row. */ i=0; while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS) { i++; printf("%d: szCol1 = %s (%d)\n",i,szCol1, length); } if (retcode != SQL_NO_DATA_FOUND) { printStatementError(hstmt, "problem with SQLFetch"); return 1; } } printf("Done\n"); return 1; }
void CODBCCreate::accept() { if ( fds->isOn()) { QString conn_str; char out_str[ 4095 ]; const char *in_str; SQLHENV henv; SQLHDBC hdbc; SQLSMALLINT len; SQLRETURN ret; QString fname = file_edit->text(); // Make sure it ends with .dsn if ( fname.right( 4 ).lower().compare( ".dsn" )) { fname.append( ".dsn" ); file_edit->setText( fname ); } conn_str = "DRIVER={" + current_driver + "};SAVEFILE=" + fname + ";"; if ( extra_keywords.length() > 0 ) { int start = 0; int end = 0; while( start < extra_keywords.length() ) { end = extra_keywords.find( '\n', start ); if ( end == -1 ) { end = extra_keywords.length(); } conn_str += extra_keywords.mid( start, end-start ) + ";"; start = end + 1; } } in_str = (const char*)conn_str; if ( verify ) { SQLAllocEnv( &henv ); SQLAllocConnect( henv, &hdbc ); ret = SQLDriverConnect( hdbc, (SQLHWND)1, (SQLCHAR*)in_str, strlen( in_str ), (SQLCHAR*)out_str, sizeof( out_str ), &len, SQL_DRIVER_COMPLETE ); SQLFreeConnect( hdbc ); SQLFreeEnv( henv ); if ( ret != SQL_SUCCESS ) { int create = QMessageBox::No; create = QMessageBox::information( NULL, "Create Data Source", "A connection could not be made using the file data source parameters entered. Save non-verified file DSN?", QMessageBox::Yes, QMessageBox::No ); if ( create == QMessageBox::No ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_CREATE_DSN_FAILED, "" ); ret_code = false; #ifdef QT_V4LAYOUT Q3Wizard::reject(); #else QWizard::reject(); #endif return; } else { strcpy( out_str, in_str ); if ( !createDsn()) { ret_code = false; #ifdef QT_V4LAYOUT Q3Wizard::reject(); #else QWizard::reject(); #endif return; } } } } else { strcpy( out_str, in_str ); if ( !createDsn()) { ret_code = false; #ifdef QT_V4LAYOUT Q3Wizard::reject(); #else QWizard::reject(); #endif return; } } ret_code = true; } else { int mode; if ( sds -> isOn()) { mode = ODBC_ADD_SYS_DSN; } else { mode = ODBC_ADD_DSN; } if ( dsn.length() > 0 ) { ret_code = SQLConfigDataSource((HWND) 1, mode, current_driver, dsn.prepend( "DSN=" )); } else { ret_code = SQLConfigDataSource((HWND) 1, mode, current_driver, "" ); } } #ifdef QT_V4LAYOUT Q3Wizard::accept(); #else QWizard::accept(); #endif }
static int backsql_open_db_handle( backsql_info *bi, SQLHDBC *dbhp ) { /* TimesTen */ char DBMSName[ 32 ]; int rc; assert( dbhp != NULL ); *dbhp = SQL_NULL_HDBC; Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_handle()\n", 0, 0, 0 ); rc = SQLAllocConnect( bi->sql_db_env, dbhp ); if ( !BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): " "SQLAllocConnect() failed:\n", 0, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, SQL_NULL_HDBC, SQL_NULL_HENV, rc ); return LDAP_UNAVAILABLE; } rc = SQLConnect( *dbhp, (SQLCHAR*)bi->sql_dbname, SQL_NTS, (SQLCHAR*)bi->sql_dbuser, SQL_NTS, (SQLCHAR*)bi->sql_dbpasswd, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): " "SQLConnect() to database \"%s\" %s.\n", bi->sql_dbname, rc == SQL_SUCCESS_WITH_INFO ? "succeeded with info" : "failed", 0 ); backsql_PrintErrors( bi->sql_db_env, *dbhp, SQL_NULL_HENV, rc ); if ( rc != SQL_SUCCESS_WITH_INFO ) { SQLFreeConnect( *dbhp ); return LDAP_UNAVAILABLE; } } /* * TimesTen : Turn off autocommit. We must explicitly * commit any transactions. */ SQLSetConnectOption( *dbhp, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF ); /* * See if this connection is to TimesTen. If it is, * remember that fact for later use. */ /* Assume until proven otherwise */ bi->sql_flags &= ~BSQLF_USE_REVERSE_DN; DBMSName[ 0 ] = '\0'; rc = SQLGetInfo( *dbhp, SQL_DBMS_NAME, (PTR)&DBMSName, sizeof( DBMSName ), NULL ); if ( rc == SQL_SUCCESS ) { if ( strcmp( DBMSName, "TimesTen" ) == 0 || strcmp( DBMSName, "Front-Tier" ) == 0 ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): " "TimesTen database!\n", 0, 0, 0 ); bi->sql_flags |= BSQLF_USE_REVERSE_DN; } } else { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): " "SQLGetInfo() failed.\n", 0, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, *dbhp, SQL_NULL_HENV, rc ); SQLDisconnect( *dbhp ); SQLFreeConnect( *dbhp ); return LDAP_UNAVAILABLE; } /* end TimesTen */ Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_handle()\n", 0, 0, 0 ); return LDAP_SUCCESS; }
static int backsql_open_db_conn( backsql_info *si, int ldap_cid, backsql_db_conn **pdbc ) { /* TimesTen */ char DBMSName[ 32 ]; backsql_db_conn *dbc; int rc; assert( pdbc ); *pdbc = NULL; Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_conn()\n", 0, 0, 0 ); dbc = (backsql_db_conn *)ch_calloc( 1, sizeof( backsql_db_conn ) ); dbc->ldap_cid = ldap_cid; rc = SQLAllocConnect( si->db_env, &dbc->dbh ); if ( !BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: " "SQLAllocConnect() failed:\n", 0, 0, 0 ); backsql_PrintErrors( si->db_env, SQL_NULL_HDBC, SQL_NULL_HENV, rc ); return LDAP_UNAVAILABLE; } rc = SQLConnect( dbc->dbh, si->dbname, SQL_NTS, si->dbuser, SQL_NTS, si->dbpasswd, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: " "SQLConnect() to database '%s' as user '%s' " "%s:\n", si->dbname, si->dbuser, rc == SQL_SUCCESS_WITH_INFO ? "succeeded with info" : "failed" ); backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc ); if ( rc != SQL_SUCCESS_WITH_INFO ) { return LDAP_UNAVAILABLE; } } /* * TimesTen : Turn off autocommit. We must explicitly * commit any transactions. */ SQLSetConnectOption( dbc->dbh, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF ); /* * See if this connection is to TimesTen. If it is, * remember that fact for later use. */ /* Assume until proven otherwise */ si->bsql_flags &= ~BSQLF_USE_REVERSE_DN; DBMSName[ 0 ] = '\0'; rc = SQLGetInfo( dbc->dbh, SQL_DBMS_NAME, (PTR)&DBMSName, sizeof( DBMSName ), NULL ); if ( rc == SQL_SUCCESS ) { if ( strcmp( DBMSName, "TimesTen" ) == 0 || strcmp( DBMSName, "Front-Tier" ) == 0 ) { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: " "TimesTen database!\n", 0, 0, 0 ); si->bsql_flags |= BSQLF_USE_REVERSE_DN; } } else { Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: " "SQLGetInfo() failed:\n", 0, 0, 0 ); backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc ); } /* end TimesTen */ Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(): " "connected, adding to tree\n", 0, 0, 0 ); ldap_pvt_thread_mutex_lock( &si->dbconn_mutex ); avl_insert( &si->db_conns, dbc, backsql_cmp_connid, NULL ); ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex ); Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_conn()\n", 0, 0, 0 ); *pdbc = dbc; return LDAP_SUCCESS; }
/* --- SDD METHODS --- */ static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { SQLHENV hEnv = NULL; SQLHDBC hConnect = NULL; char * szError; HB_ERRCODE errCode; #if ODBCVER >= 0x0300 if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv ) ) ) #else if( SQL_SUCCEEDED( SQLAllocEnv( &hEnv ) ) ) #endif { #if ODBCVER >= 0x0300 SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, ( SQLPOINTER ) SQL_OV_ODBC3, SQL_IS_UINTEGER ); #endif #if ODBCVER >= 0x0300 if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hConnect ) ) ) #else if( SQL_SUCCEEDED( SQLAllocConnect( hEnv, &hConnect ) ) ) #endif { const O_HB_CHAR * pchConStr; void * hConnStr; HB_SIZE nConnLen; SQLTCHAR cBuffer[ 1024 ]; SQLSMALLINT iLen = HB_SIZEOFARRAY( cBuffer ); cBuffer[ 0 ] = '\0'; pchConStr = O_HB_ARRAYGETSTR( pItem, 2, &hConnStr, &nConnLen ); if( SQL_SUCCEEDED( SQLDriverConnect( hConnect, NULL, ( SQLTCHAR * ) HB_UNCONST( pchConStr ), ( SQLSMALLINT ) nConnLen, cBuffer, HB_SIZEOFARRAY( cBuffer ), &iLen, SQL_DRIVER_NOPROMPT ) ) ) { hb_strfree( hConnStr ); pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); ( ( SDDCONN * ) pConnection->pSDDConn )->hConn = hConnect; ( ( SDDCONN * ) pConnection->pSDDConn )->hEnv = hEnv; return HB_SUCCESS; } else { hb_strfree( hConnStr ); szError = odbcGetError( hEnv, hConnect, SQL_NULL_HSTMT, &errCode ); hb_rddsqlSetError( errCode, szError, NULL, NULL, 0 ); hb_xfree( szError ); } #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_DBC, hConnect ); #else SQLFreeConnect( hConnect ); #endif } else { szError = odbcGetError( hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_CONNALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode ); hb_xfree( szError ); } #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_ENV, hEnv ); #else SQLFreeEnv( hEnv ); #endif } else { szError = odbcGetError( SQL_NULL_HENV, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_ENVALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode ); hb_xfree( szError ); } return HB_FAILURE; }
int main (int argc, char *argv[]) { if (argc < 4) { fprintf (stderr, "ERR : called as setcurs <dsn> <uid> <pwd> [<scroll>=1]\n"); exit (-3); } rc = SQLAllocEnv (&henv); if (rc != SQL_SUCCESS) { fprintf (stderr, "ERR : cannot alloc ODBC environment\n"); exit (-3); } rc = SQLAllocConnect (henv, &hdbc); CHK_ERR (SQL_NULL_HSTMT); rc = SQLConnect (hdbc, argv[1], SQL_NTS, argv[2], SQL_NTS, argv[3], SQL_NTS); CHK_ERR (SQL_NULL_HSTMT); /* Allocate the statements and set the cursor name. */ rc = SQLAllocStmt (hdbc, &hstmtSelect); CHK_ERR (SQL_NULL_HSTMT); rc = SQLAllocStmt (hdbc, &hstmtUpdate); CHK_ERR (SQL_NULL_HSTMT); if (argc <= 4 || atoi (argv[4]) != 0) { rc = SQLSetStmtOption (hstmtSelect, SQL_CURSOR_TYPE, SQL_CURSOR_STATIC); CHK_ERR (hstmtSelect); rc = SQLSetScrollOptions (hstmtSelect, SQL_CONCUR_READ_ONLY, 1, 1); CHK_ERR (hstmtSelect); } rc = SQLSetCursorName (hstmtSelect, "C1", SQL_NTS); CHK_ERR (hstmtSelect); /* SELECT the result set and bind its columns to local buffers. */ rc = SQLExecDirect (hstmtSelect, "SELECT NAME,PHONE from GOGO", SQL_NTS); CHK_ERR (hstmtSelect); rc = SQLBindCol (hstmtSelect, 1, SQL_C_CHAR, szName, NAME_LEN, &cbName); CHK_ERR (hstmtSelect); rc = SQLBindCol (hstmtSelect, 2, SQL_C_CHAR, szPhone, PHONE_LEN, &cbPhone); CHK_ERR (hstmtSelect); /* Read through the result set until the cursor is */ /* positioned on the row for John Smith. */ do retcode = SQLFetch (hstmtSelect); while ((retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) && (strcmp (szName, "Smith, John") != 0)); rc = retcode; CHK_ERR (hstmtSelect); /* Perform a positioned update of John Smith's name. */ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { rc = SQLExecDirect (hstmtUpdate, "UPDATE GOGO SET PHONE='2064890154' WHERE CURRENT OF C1", SQL_NTS); CHK_ERR (hstmtUpdate); } #if 0 SQLFreeStmt (hstmtUpdate, SQL_DROP); SQLFreeStmt (hstmtSelect, SQL_DROP); SQLDisconnect (hdbc); SQLFreeConnect (hdbc); #endif printf ("PASSED: SetCursorName Test\n"); exit (0); }
int ODBC_Connect (char *connStr) { short buflen; SQLCHAR dataSource[1024]; SQLTCHAR dsn[33]; SQLTCHAR desc[255]; SQLTCHAR driverInfo[255]; SQLSMALLINT len1, len2; int status; #ifdef UNICODE SQLWCHAR wdataSource[1024]; #endif #if (ODBCVER < 0x0300) if (SQLAllocEnv (&henv) != SQL_SUCCESS) return -1; if (SQLAllocConnect (henv, &hdbc) != SQL_SUCCESS) return -1; #else if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv) != SQL_SUCCESS) return -1; SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); if (SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS) return -1; #endif /* * Set the application name */ SQLSetConnectOption (hdbc, SQL_APPLICATION_NAME, (SQLULEN) TEXT ("odbctest")); /* * Show the version number of the driver manager */ status = SQLGetInfo (hdbc, SQL_DM_VER, driverInfo, sizeof (driverInfo), &len1); if (status == SQL_SUCCESS) { #ifdef UNICODE printf ("Driver Manager: %S\n", driverInfo); #else printf ("Driver Manager: %s\n", driverInfo); #endif } /* * Either use the connect string provided on the command line or * ask for one. If an empty string or a ? is given, show a nice * list of options */ if (connStr && *connStr) strcpy ((char *) dataSource, connStr); else while (1) { /* * Ask for the connect string */ printf ("\nEnter ODBC connect string (? shows list): "); if (fgets ((char *) dataSource, sizeof (dataSource), stdin) == NULL) return 1; /* * Remove trailing '\n' */ dataSource[strlen ((char *) dataSource) - 1] = '\0'; /* * Check if the user wants to quit */ if (!strcmp ((char *)dataSource, "quit") || !strcmp ((char *)dataSource, "exit")) return -1; /* * If the user entered something other than a ? * break out of the while loop */ if (*dataSource && *dataSource != '?') break; /* * Print headers */ fprintf (stderr, "\n%-32s | %-40s\n", "DSN", "Driver"); fprintf (stderr, "------------------------------------------------------------------------------\n"); /* * Goto the first record */ if (SQLDataSources (henv, SQL_FETCH_FIRST, dsn, NUMTCHAR (dsn), &len1, desc, NUMTCHAR (desc), &len2) != SQL_SUCCESS) continue; /* * Show all records */ do { #ifdef UNICODE fprintf (stderr, "%-32S | %-40S\n", dsn, desc); #else fprintf (stderr, "%-32s | %-40s\n", dsn, desc); #endif } while (SQLDataSources (henv, SQL_FETCH_NEXT, dsn, NUMTCHAR (dsn), &len1, desc, NUMTCHAR (desc), &len2) == SQL_SUCCESS); } #ifdef UNICODE strcpy_A2W (wdataSource, (char *) dataSource); status = SQLDriverConnectW (hdbc, 0, (SQLWCHAR *) wdataSource, SQL_NTS, (SQLWCHAR *) outdsn, NUMTCHAR (outdsn), &buflen, SQL_DRIVER_COMPLETE); if (status != SQL_SUCCESS) ODBC_Errors ("SQLDriverConnectW"); #else status = SQLDriverConnect (hdbc, 0, (SQLCHAR *) dataSource, SQL_NTS, (SQLCHAR *) outdsn, NUMTCHAR (outdsn), &buflen, SQL_DRIVER_COMPLETE); if (status != SQL_SUCCESS) ODBC_Errors ("SQLDriverConnect"); #endif if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO) return -1; connected = 1; /* * Print out the version number and the name of the connected driver */ status = SQLGetInfo (hdbc, SQL_DRIVER_VER, driverInfo, NUMTCHAR (driverInfo), &len1); if (status == SQL_SUCCESS) { #ifdef UNICODE printf ("Driver: %S", driverInfo); #else printf ("Driver: %s", driverInfo); #endif status = SQLGetInfo (hdbc, SQL_DRIVER_NAME, driverInfo, NUMTCHAR (driverInfo), &len1); if (status == SQL_SUCCESS) { #ifdef UNICODE printf (" (%S)", driverInfo); #else printf (" (%s)", driverInfo); #endif } printf ("\n"); } /* * Show the list of supported functions in trace log */ #if (ODBCVER < 0x0300) { SQLUSMALLINT exists[100]; SQLGetFunctions (hdbc, SQL_API_ALL_FUNCTIONS, exists); } #else { SQLUSMALLINT exists[SQL_API_ODBC3_ALL_FUNCTIONS_SIZE]; SQLGetFunctions (hdbc, SQL_API_ODBC3_ALL_FUNCTIONS, exists); } #endif /* * Allocate statement handle */ #if (ODBCVER < 0x0300) if (SQLAllocStmt (hdbc, &hstmt) != SQL_SUCCESS) return -1; #else if (SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS) return -1; #endif return 0; }
/* ------------------------------------------------------------- */ static PJXSQLCONNECT jx_sqlNewConnection(void ) { // static SQLHSTMT hstmt = 0 ; // SQLINTEGEREGER len; // UCHAR Label [256]; // LGL err = ON; // LONG rows =0; PJXSQLCONNECT pConnection; LONG attrParm; PUCHAR server = "*LOCAL"; int rc; PSQLOPTIONS po; pConnection = memAlloc(sizeof(JXSQLCONNECT)); memset(pConnection , 0 , sizeof(JXSQLCONNECT)); pConnection->pCd = XlateXdOpen (13488, 0); po = &pConnection->options; po->upperCaseColName = OFF; po->autoParseContent = ON; po->DecimalPoint = '.'; po->hexSort = OFF; po->sqlNaming = OFF; po->DateSep = '-'; po->DateFmt = 'y'; po->TimeSep = ':'; po->TimeFmt = 'H'; // allocate an environment handle rc = SQLAllocEnv (&pConnection->henv); if (rc != SQL_SUCCESS ) { check_error (NULL); jx_sqlDisconnect (pConnection); return NULL; // we have an error } // Note - this is invers: Default to IBMi naming attrParm = pConnection->options.sqlNaming == ON ? SQL_FALSE : SQL_TRUE; rc = SQLSetEnvAttr (pConnection->henv, SQL_ATTR_SYS_NAMING, &attrParm , 0); if (rc != SQL_SUCCESS ) { check_error (NULL); jx_sqlDisconnect (pConnection); return NULL; // we have an error } /* attrParm = SQL_TRUE; rc = SQLSetEnvAttr (pConnection->henv, SQL_ATTR_UTF8 , &attrParm , 0); if (pSQL->rc != SQL_SUCCESS ) { jx_sqlDisconnect (pConnection); return NULL; // we have an error } */ rc = SQLAllocConnect (pConnection->henv, &pConnection->hdbc); // allocate a connection handle if (rc != SQL_SUCCESS ) { check_error (NULL); jx_sqlDisconnect (pConnection); return NULL; // we have an error } attrParm = SQL_TXN_NO_COMMIT; rc = SQLSetConnectAttr (pConnection->hdbc, SQL_ATTR_COMMIT , &attrParm , 0); if (rc != SQL_SUCCESS ) { check_error (NULL); jx_sqlDisconnect (pConnection); return NULL; // we have an error } rc = SQLConnect (pConnection->hdbc, server , SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS); if (rc != SQL_SUCCESS ) { check_error (NULL); jx_sqlDisconnect (pConnection); return NULL; // we have an error } return pConnection; // we are ok }
STDMETHODIMP CComSpySqlAudit::Init(LPCWSTR pwszDSN, LPCWSTR pwszUser, LPCWSTR pwszPw) { WCHAR wszBuffer[1024]; SQLRETURN rc; if (m_henv || m_hstmt || m_hdbc) return E_UNEXPECTED; //did you call Init() twice? rc = SQLAllocEnv( &m_henv ); if (!SQL_SUCCEEDED(rc)) { ATLTRACE(L"CComSpySqlAudit::Init() - SQLAllocEnv failed with code %d\n", rc); goto Error; } //Allocate the Connection handle rc = SQLAllocConnect( m_henv, &m_hdbc ); if (!SQL_SUCCEEDED(rc)) { ATLTRACE(L"CComSpySqlAudit::Init() - SQLAllocConnect failed with code %d\n", rc); goto Error; } SQLSetConnectOption(m_hdbc, SQL_LOGIN_TIMEOUT, 5); rc = SQLConnect( m_hdbc, SqlStringArg(pwszDSN), SQL_NTS, SqlStringArg(pwszUser), SQL_NTS, SqlStringArg(pwszPw), SQL_NTS); if (!SQL_SUCCEEDED(rc)) { ATLTRACE(L"CComSpySqlAudit::Init() - SQLConnect failed with code %d\n", rc); goto Error; } //Allocate the statement handle rc = SQLAllocStmt( m_hdbc, &m_hstmt); if (!SQL_SUCCEEDED(rc)) { ATLTRACE(L"CComSpySqlAudit::Init() - SQLAllocStmt failed with code %d\n", rc); goto Error; } // // verify the table existence // { for (int i=0; i < ARRAYSIZE(pwszTables); i++) { HRESULT hrString = StringCchPrintfEx( wszBuffer, ARRAYSIZE(wszBuffer), NULL, NULL, STRSAFE_NO_TRUNCATION, L"CREATE TABLE dbo.%s (Ct int NOT NULL )", pwszTables[i]); _ASSERTE(SUCCEEDED(hrString)); rc = SQLExecDirect(m_hstmt, SqlStringArg(wszBuffer), SQL_NTS); if (SQL_SUCCEEDED(rc)) { // this means the table is not in the db //Clean up the table just created and exit hrString = StringCchPrintfEx(wszBuffer, ARRAYSIZE(wszBuffer), NULL, NULL, STRSAFE_NO_TRUNCATION, L"drop table %s", pwszTables[i]); _ASSERTE(SUCCEEDED(hrString)); rc = SQLExecDirect(m_hstmt, SqlStringArg(wszBuffer), SQL_NTS); ATLTRACE(L"Table %s is not in the Database.\n", pwszTables[i]); goto Error; } } } return S_OK; Error: if (m_hstmt) { SQLFreeStmt( m_hstmt, SQL_DROP ); } if (m_hdbc) { rc = SQLDisconnect( m_hdbc); SQLFreeConnect( m_hdbc); if (m_henv) SQLFreeEnv( m_henv ); } m_hdbc = m_hstmt = m_henv = NULL; return E_FAIL; }
/**************************** * OpenDatabase - do everything we have to do to get a viable connection to szDSN ***************************/ static int OpenDatabase( SQLHENV *phEnv, SQLHDBC *phDbc, char *szDSN, char *szUID, char *szPWD ) { if ( version3 ) { if ( SQLAllocHandle( SQL_HANDLE_ENV, NULL, phEnv ) != SQL_SUCCESS ) { fprintf( stderr, "[ISQL]ERROR: Could not SQLAllocHandle( SQL_HANDLE_ENV )\n" ); return 0; } if ( SQLSetEnvAttr( *phEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) 3, 0 ) != SQL_SUCCESS ) { if ( bVerbose ) DumpODBCLog( hEnv, 0, 0 ); fprintf( stderr, "[ISQL]ERROR: Could not SQLSetEnvAttr( SQL_HANDLE_DBC )\n" ); SQLFreeHandle( SQL_HANDLE_ENV, *phEnv ); return 0; } if ( SQLAllocHandle( SQL_HANDLE_DBC, *phEnv, phDbc ) != SQL_SUCCESS ) { if ( bVerbose ) DumpODBCLog( hEnv, 0, 0 ); fprintf( stderr, "[ISQL]ERROR: Could not SQLAllocHandle( SQL_HANDLE_DBC )\n" ); SQLFreeHandle( SQL_HANDLE_ENV, *phEnv ); return 0; } if ( !SQL_SUCCEEDED( SQLConnect( *phDbc, (SQLCHAR*)szDSN, SQL_NTS, (SQLCHAR*)szUID, SQL_NTS, (SQLCHAR*)szPWD, SQL_NTS ))) { if ( bVerbose ) DumpODBCLog( hEnv, hDbc, 0 ); fprintf( stderr, "[ISQL]ERROR: Could not SQLConnect\n" ); SQLFreeHandle( SQL_HANDLE_DBC, *phDbc ); SQLFreeHandle( SQL_HANDLE_ENV, *phEnv ); return 0; } } else { if ( SQLAllocEnv( phEnv ) != SQL_SUCCESS ) { fprintf( stderr, "[ISQL]ERROR: Could not SQLAllocEnv\n" ); return 0; } if ( SQLAllocConnect( *phEnv, phDbc ) != SQL_SUCCESS ) { if ( bVerbose ) DumpODBCLog( hEnv, 0, 0 ); fprintf( stderr, "[ISQL]ERROR: Could not SQLAllocConnect\n" ); SQLFreeEnv( *phEnv ); return 0; } if ( !SQL_SUCCEEDED( SQLConnect( *phDbc, (SQLCHAR*)szDSN, SQL_NTS, (SQLCHAR*)szUID, SQL_NTS, (SQLCHAR*)szPWD, SQL_NTS ))) { if ( bVerbose ) DumpODBCLog( hEnv, hDbc, 0 ); fprintf( stderr, "[ISQL]ERROR: Could not SQLConnect\n" ); SQLFreeConnect( *phDbc ); SQLFreeEnv( *phEnv ); return 0; } } /* * does the driver support SQLMoreResults */ if ( !SQL_SUCCEEDED( SQLGetFunctions( *phDbc, SQL_API_SQLMORERESULTS, &has_moreresults ))) { has_moreresults = 0; } return 1; }
extern int FAR PASCAL _export GrabData(LPSAFEARRAY FAR *ppsa) { long Ubound; // Upper bound of array long ix[255]; // Array to hold names int i, retcode; BSTR element = NULL; // BSTR to place back into array char szData[50]; // Array to hold individual data SDWORD dwNameLength; // length of returned field HENV henv; // Handle to an ODBC environment HDBC hdbc; // Handle to an ODBC connection HSTMT hStmt; // Handle to an ODBC statement // retrieve the number of elements passed in, // assume a one dimensional array. SafeArrayGetUBound (*ppsa, 1, &Ubound); // Fill array with indexes for (i=0;i<Ubound;i++) ix[i] = i; // Allocate ODBC environment retcode = SQLAllocEnv(&henv); if (retcode) return (-1); // Allocate the connection retcode = SQLAllocConnect(henv, &hdbc); if (retcode) return (-1); // Connect retcode = SQLConnect(hdbc,(UCHAR far*)"Customers", SQL_NTS,NULL, SQL_NTS, NULL, SQL_NTS); if (retcode) return (-1); // Allocate statement retcode = SQLAllocStmt(hdbc, &hStmt); if (retcode) return (-1); // Execute the statement retcode = SQLExecDirect(hStmt, (UCHAR far*)"SELECT [CONTACT_NA] FROM Customer ORDER BY [CONTACT_NA]", SQL_NTS); if (retcode) return (-1); i = 0; // Bind The column SQLBindCol(hStmt, 1, SQL_C_CHAR, &szData, 30, &dwNameLength); // build the data into the array while (!retcode) { retcode=SQLFetch(hStmt); element = SysAllocString((BSTR)szData); if (!retcode) SafeArrayPutElement(*ppsa, &ix[i], element); SysFreeString (element); i++; } // Free statement SQLFreeStmt(hStmt, SQL_DROP); // Disconnect from database SQLDisconnect(hdbc); // Free connection SQLFreeConnect(hdbc); // Free environment SQLFreeEnv(henv); return (1); }
int main(int argc, char** argv) { char buffer[BUFSIZE]; char result[BUFSIZE]; char *dsn = getenv("DSN"); char *uid = getenv("UID"); char *pwd = getenv("PWD"); if (SQL_SUCCESS != (rc = SQLAllocEnv (&henv))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLAllocConnect (henv, &hdbc))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLConnect (hdbc, dsn ? dsn : "1111", SQL_NTS, uid ? uid : "dba", SQL_NTS, pwd ? pwd : "dba", SQL_NTS))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLAllocStmt (hdbc, &hstmt))) { odbc_error (); return -1; } while (NULL != fgets (buffer, BUFSIZE, stdin)) { if (SQL_SUCCESS != (rc = SQLBindParameter (hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_CHAR, sizeof (result), 0, result, sizeof (result), NULL))) { odbc_error (); break; } if (0 == strncmp ("chdir", buffer, 5)) { if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (buffer + 6), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_CHDIR (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } /*change directory:*/ printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("stylesheet", buffer, 10)) { /*load stylesheet: */ if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (&buffer[11]), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_STYLESHEET (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("input", buffer, 5)) { if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (&buffer[6]), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_INPUT (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("transform", buffer, 9)) { short iter = 1; char * c = buffer+10; char *filename; timer_account_t ta; /* get output filename */ while (*c != '\n' && *c != '\0' && *c != ' ') { c++; } if (*c == ' ') { *c = 0; c++; filename = buffer + 10; } /* get # of iterations */ iter = atoi(c); if (iter <= 0) iter = 1; /* printf ("file: %s iter : %d buffer %s\n", filename, iter, buffer); */ ta_init (&ta, "test"); ta_enter (&ta); if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, filename, NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_SHORT, SQL_INTEGER, 0, 0, &iter, NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_TRANSFORM (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } ta_leave (&ta); printf ("OK wallclock: %06ld ms; cpuclock: %06ld'\n", ta.ta_total, ta.ta_total); fflush (stdout); } else if (0 == strncmp ("terminate", buffer, 9)) { printf ("OK\n"); break; } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_CLOSE))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS))) { odbc_error (); return -1; } } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_DROP))) return -1; if (SQL_SUCCESS != (rc = SQLDisconnect (hdbc))) return -1; if (SQL_SUCCESS != (rc = SQLFreeConnect (hdbc))) return -1; if (SQL_SUCCESS != (rc = SQLFreeEnv (henv))) return -1; return 0; }
int main (int argc, char ** argv) { char* uid = "dba", * pwd = "dba"; if (argc < 2) { printf ("Usage: scroll dsn delete-n [user] [password]\n"); exit (1); } if (argc > 2) { uid = argv [3]; pwd = argv [4]; } SQLAllocEnv (& henv); SQLAllocConnect (henv, &hdbc1); SQLAllocConnect (henv, &hdbc2); if (SQL_ERROR == SQLConnect (hdbc1, (UCHAR *) argv [1], SQL_NTS, (UCHAR *) uid, SQL_NTS, (UCHAR *) pwd, SQL_NTS)) { print_error (SQL_NULL_HENV, hdbc1, SQL_NULL_HSTMT); exit (1); } if (SQL_ERROR == SQLConnect (hdbc2, (UCHAR *) argv [1], SQL_NTS, (UCHAR *) uid, SQL_NTS, (UCHAR *) pwd, SQL_NTS)) { print_error (SQL_NULL_HENV, hdbc2, SQL_NULL_HSTMT); exit (1); } t1_init (); tsc_lock (SQL_CURSOR_DYNAMIC); tsc_bm (SQL_CURSOR_STATIC, 0, 1); tsc_bm (SQL_CURSOR_KEYSET_DRIVEN, 5, 1); tsc_bm (SQL_CURSOR_KEYSET_DRIVEN, 0, 1); tsc_bm (SQL_CURSOR_DYNAMIC, 0, 1); tsc_values (SQL_CURSOR_DYNAMIC); tsc_values (SQL_CURSOR_KEYSET_DRIVEN); tsc_pos (SQL_CURSOR_KEYSET_DRIVEN, T1_BASE_TEXT); tsc_pos (SQL_CURSOR_DYNAMIC, T1_BASE_TEXT); #if 0 tsc_co (SQL_CURSOR_DYNAMIC, 0, SQL_AUTOCOMMIT_ON); tsc_co (SQL_CURSOR_DYNAMIC, 0, 0); tsc_co (SQL_CURSOR_FORWARD_ONLY, 0, 0); #endif tsc_fwd (); tsc_scroll (SQL_CURSOR_STATIC, 0, 1, 0, 2, T1_BASE_TEXT); tsc_scroll (SQL_CURSOR_DYNAMIC, 0, 1, 0, 2, T1_BASE_TEXT); tsc_scroll (SQL_CURSOR_DYNAMIC, 0, 0, 0, 2, T1_BASE_TEXT); tsc_scroll (SQL_CURSOR_KEYSET_DRIVEN, 7, 1, 0, 2, T1_BASE_TEXT); tsc_scroll (SQL_CURSOR_KEYSET_DRIVEN, 5, 1, 0, 2, T1_BASE_TEXT); tsc_scroll (SQL_CURSOR_KEYSET_DRIVEN, 2000, 1, 0, 2, T1_BASE_TEXT); tw_exec (&tw1, "delete from T1"); t1_lines (&tw1, 0, 1100, 100); tsc_readtable (SQL_CURSOR_STATIC,0,1,0,2,T1_BASE_TEXT); tsc_readtable (SQL_CURSOR_DYNAMIC,0,1,0,2,T1_BASE_TEXT); /* tsc_update (SQL_CURSOR_KEYSET_DRIVEN, SQL_CONCUR_VALUES, T1_BASE_TEXT); */ tsc_commit_cursor (&tw1, SQL_CURSOR_FORWARD_ONLY, SQL_COMMIT); tsc_commit_cursor (&tw2, SQL_CURSOR_FORWARD_ONLY, SQL_ROLLBACK); tsc_commit_cursor (&tw1, SQL_CURSOR_STATIC, SQL_COMMIT); tsc_commit_cursor (&tw1, SQL_CURSOR_STATIC, SQL_ROLLBACK); tsc_commit_cursor (&tw1, SQL_CURSOR_DYNAMIC, SQL_COMMIT); tsc_commit_cursor (&tw2, SQL_CURSOR_DYNAMIC, SQL_ROLLBACK); printf ("========== SCROLL TEST COMPLETE\n"); SQLDisconnect (hdbc1); SQLDisconnect (hdbc2); SQLFreeConnect (hdbc1); SQLFreeConnect (hdbc2); return 0; }