简体   繁体   English

在C ++控制台应用程序中编写sql查询?

[英]writing sql query in c++ console application?

I have been trying to write sql query through my c++ code but Im unable to do this. 我一直在尝试通过我的C ++代码编写sql查询,但是我无法做到这一点。 I have connected the server successfully but dont know the exact way of writing queries to the specific db. 我已成功连接服务器,但不知道将查询写入特定数据库的确切方法。 My source code is as: 我的源代码为:

#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();
}

I need some guidance . 我需要一些指导。 Hopes for suggestion 希望提出建议

我从来没有用c / c ++编写sql查询,但这也许可以为您提供帮助: http : //cs.dvc.edu/HowTo_SQL.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM