如何连接到sql server 2008并使用visual studio 2008从表中获取数据

如何连接到sql server 2008并使用visual studio 2008从表中获取数据

问题描述:



我想从visual studio 2008连接到sql server并从任何表中获取数据。我需要使用c ++来做。我怎么能做到这一点?我不能使用任何管理代码。



这就是我现在正在尝试的:我从另一个参考文献中得到它。

在此代码中,从连接(SQLDriverConnect(),函数)开始,它显示错误。

Hi,
I wants to connect to sql server from visual studio 2008 and fetch data from any table. I need to do it using c++. How i can achieve this? I cna't use any Managed code.

This what i am trying now: I got it from another reference.
In this code, from connection (SQLDriverConnect(), function) onwards its shows errors.

#include "stdafx.h"
#include <windows.h>

#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sqlncli.h>

#include <cstdio>
#include <string>

const int MAX_CHAR = 1024;

bool test_retcode( RETCODE my_code, const char* my_message )
{
    
	bool success = ( my_code == SQL_SUCCESS_WITH_INFO || my_code == SQL_SUCCESS );
	
    if ( ! success )
    {
        printf( "%s", my_message );
    }

    return success;
}

int main ( )
{
    unsigned char connStrOut[256];
	SQLSMALLINT bufsize=0;

	SQLHENV EnvironmentHandle;
	
    RETCODE retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvironmentHandle );
    test_retcode( retcode, "SQLAllocHandle(Env) failed!" );

    retcode = SQLSetEnvAttr( EnvironmentHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER );
    test_retcode( retcode, "SQLSetEnvAttr(ODBC version) Failed" );

    SQLHDBC ConnHandle;
	
	retcode = SQLAllocHandle( SQL_HANDLE_DBC_INFO_TOKEN, EnvironmentHandle, &ConnHandle );
    test_retcode( retcode, "Could not allocate MS SQL 2000 connection handle." );

    SQLSMALLINT driver_out_length;
	
    retcode = SQLDriverConnect( ConnHandle, NULL,(SQLWCHAR*) "DRIVER={SQL Server};SERVER=(local);UID=labops;PWD=99w1ldf1re;Database=labops;", SQL_NTS, (SQLWCHAR*)connStrOut, 256, &driver_out_length, SQL_DRIVER_NOPROMPT );
	test_retcode( retcode, "SQLConnect() Failed" );
	
    SQLHSTMT StatementHandle;
    retcode = SQLAllocHandle(SQL_HANDLE_STMT, ConnHandle, &StatementHandle);
    test_retcode( retcode, "Failed to allocate SQL Statement handle." );

    char* buffer = new char[ MAX_CHAR ];
    SQLINTEGER length = MAX_CHAR - 1;

   
    retcode = SQLBindCol( StatementHandle, 1, SQL_C_CHAR,(SQLPOINTER) NULL,(SQLINTEGER) SQL_DATA_AT_EXEC,&length );

    test_retcode( retcode, "Failed to bind column." );
    

    retcode = SQLExecDirect( StatementHandle, (SQLWCHAR*) "SELECT * FROM LabopsStatus", SQL_NTS );
    test_retcode( retcode, "SQLExecDirect failed." );
	getchar();
    
    retcode = SQLFetch( StatementHandle );
    if ( retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO && retcode != SQL_NO_DATA )
    {
        printf( "Problem fetching row.\n" );
        return 9;
    }

    printf( "Fetched data.  length: %d\n", length );
    


    bool sql_success;
    std::string data;
    RETCODE r2;
    do
    {
        r2 = SQLGetData( StatementHandle, 1, SQL_C_CHAR, buffer, MAX_CHAR, &length );

        if ( sql_success = test_retcode( r2, "SQLGetData failed." ) )
        {
            data.append( buffer );
        }
        else
        {
            char* err_msg = new char[ MAX_CHAR ];
            SQLSMALLINT req = 1;
            SQLCHAR state[6];
            SQLINTEGER error;
            SQLINTEGER output_length;

            int sql_state = SQLGetDiagRec( SQL_HANDLE_STMT, StatementHandle, req, (SQLWCHAR*)state, &error, (SQLWCHAR*) err_msg, (SQLINTEGER) MAX_CHAR, (SQLSMALLINT*) &output_length );

            // state is: 07009, error_msg: "[Microsoft][SQL Native Client]Invalid Descriptor Index"
            printf( "%s\n", err_msg );

            delete err_msg;

            return 9;
        }
    } 
    while ( sql_success && r2 != SQL_SUCCESS );

    printf( "Done.\n" );
	getchar();
    return 0;
} 

您是否尝试过: SQLConnection类 [ ^ ]
Have you tried: SQLConnection Class[^]