2014-11-17 71 views
2

我一直在嘗試通過我的C++代碼編寫sql查詢,但我無法做到這一點。我已成功連接服務器,但不知道將查詢寫入特定數據庫的確切方式。我的源代碼是:在C++控制檯應用程序中編寫sql查詢?

#include <windows.h> 
#include <stdio.h> // for printf 
#include <sqlext.h>// main SQLAPI++ header 
#include <iostream> 
#include <conio.h> 
#include <string.h> 
#include <sqltypes.h> 
#include<ATLComTime.h> 

using namespace std; 
void main(int argc, char* argv[]) 
{ 
    SQLHANDLE henv; 
    SQLRETURN rc; 
    SQLHANDLE hconn; 
    SQLSMALLINT bufsize=0; 
    SQLINTEGER nativeerror=0; 
    SQLSMALLINT textlen=0; 
    unsigned char connStrOut[256]; 
    SQLWCHAR sqlstate[32]; 
    SQLWCHAR message[256]; 

    rc = SQLAllocEnv(&henv); 
    if (rc != SQL_SUCCESS) 
    { 
     printf("\nSQLAllocEnv call failed."); 
     return; 
    } 

    rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hconn); 
    if (rc != SQL_SUCCESS) 
    { 
     SQLFreeHandle(SQL_HANDLE_ENV, henv); 
     printf("\nSQLAllocHandle call failed."); 
     return; 
    } 

    rc = SQLDriverConnect(hconn, NULL, (SQLWCHAR*)TEXT("DRIVER=SQL Server;SERVER=M1-010;DATABASE=GlobalVideoProcessing;UID=AXACT\\muhammad.ahsan;PWD=Axact123;Trusted_Connection=yes;"), SQL_NTS, NULL, 256, &bufsize, SQL_DRIVER_NOPROMPT); 
    if (bufsize!=0) 
    { 
     printf("Connected successfully.\n"); 
     SQLDisconnect(hconn); 
    } 
    else 
    { 
     rc = SQLGetDiagRec(SQL_HANDLE_DBC, hconn, 1, sqlstate, &nativeerror, message, 256, &textlen); 

     printf("SQLDriverConnect failed.\n"); 
     if (rc!=SQL_ERROR) 
      printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message); 
    } 

    SQLFreeHandle(SQL_HANDLE_DBC, hconn); 
    SQLFreeHandle(SQL_HANDLE_ENV, henv); 
    getch(); 
} 

我需要一些指導。建議的希望

回答

相關問題