请问数据库的高手,用VC实现一个ODBC的程序,请给我个简单的例子?程序内容最好包含数据的录入,删除,编辑,更新等操作,里面用SQL语句来实现,是用数据库对象来实现,而不是用绑定的形式。或者推荐个网站?有关介绍用VC做数据库的。

解决方案 »

  1.   

    www.vckbase.com上面有的是例子!
      

  2.   

    #include <windows.h>
    #include <stdio.h>
    #include <sql.h>
    #include <sqlext.h>void CreateTable(HSTMT);main()
    { RETCODE rc;
    HENV henv;
    HDBC hdbc;
    HSTMT hstmt;// Variables for SQLConnect
    char * dsn = "UniteDB";
    char * uid = "InterSvr";
    char * pwd = "InterSvr";// Variables for SQLBindCol
    char m_charOddScale[20] = {"\0"};
    char m_charNoScale[20] = {"\0"};
    char m_charTestChar[21] = {"\0"};
    long sqlnts = SQL_NTS;// miscellaneous variables
    char strOutput[100] = {"\0"};
    char * SQLStr = "select OddScale, NoScale, Testchar from ScaleTable";
    // Allocate ODBC handles and connect to Oracle
    rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
    rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
    rc = SQLSetEnvAttr(henv,SQL_ATTR_CONNECTION_TIMEOUT,(void*)1000,0);
    rc = SQLSetEnvAttr(henv,SQL_ATTR_LOGIN_TIMEOUT,(void*)SQL_LOGIN_TIMEOUT_DEFAULT,0);
    rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc); rc = SQLConnect(hdbc, (unsigned char *)dsn,
    SQL_NTS, (unsigned char *)uid,
    SQL_NTS, (unsigned char *)pwd, SQL_NTS); rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_MAX_ROWS,(void *)2,0);
    // Table creation function
    CreateTable(hstmt);
    // Execute the statement and bind the column as SQL_C_CHAR
    rc = SQLExecDirect(hstmt, (unsigned char *)SQLStr, SQL_NTS);
    //rc = SQLColAttribute(hstmt, 3, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, (SQLPOINTER)&sqlnts); rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, m_charOddScale, 20, &sqlnts);
    rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, m_charNoScale, 20, &sqlnts);
    rc = SQLBindCol(hstmt, 3, SQL_C_CHAR, m_charTestChar, sizeof(m_charTestChar), &sqlnts); printf("\nOddScale (3) and NoScale as SQL_C_CHAR\n\n");// Fetch records and print the results
    FILE *fp = NULL;
    fp = fopen("test.txt","w");
    while (SQLFetch(hstmt) != SQL_NO_DATA_FOUND)
    { //memset(strOutput,' ',sizeof(strOutput));
    //strncpy(strOutput, m_charOddScale, strlen(m_charOddScale));
    //strncpy(&strOutput[15],m_charNoScale, strlen(m_charNoScale)+1);
    //strncpy(&strOutput[]) printf("\t%s\t%s\t%sEND\n",m_charOddScale,m_charNoScale,m_charTestChar);//strOutput);
    fprintf(fp,"\t%s\t%s\t%sEND\n",m_charOddScale,m_charNoScale,m_charTestChar); }
    fclose(fp);// Cleanup
    SQLFreeStmt(hstmt, SQL_CLOSE);
    SQLFreeStmt(hstmt, SQL_DROP);
    SQLDisconnect(hdbc);
    SQLFreeConnect(hdbc);
    SQLFreeEnv(henv);
    return(TRUE);
    };//------- CreateTable() ----------------void CreateTable(HSTMT hstmt)
    { RETCODE rc = 0;
    char SqlStatements[6][255] = 
    {"Drop table ScaleTable",
     "Create table ScaleTable (OddScale numeric(10,3), NoScale numeric, testchar char(20))",
     "Insert into ScaleTable values (1.1, 1.1,'12345       1 ')",
     "Insert into ScaleTable values (1.12, 1.12,'12345     1')",
     "Insert into ScaleTable values (1.123, 1.123,'12345    1')",
     "Insert into ScaleTable values (1.005, 1.005,'1234567890')"}; rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[0], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[1], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[2], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[3], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[4], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[5], SQL_NTS);
    SQLFreeStmt(hstmt, SQL_CLOSE);
    }
      

  3.   

    knight_zhuge(knight_zhuge)的例子很经典,使用API做的吧,
    但是有的地方看不懂,能不能给点注视,或者说明阿。rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
    rc = SQLSetEnvAttr(henv,SQL_ATTR_CONNECTION_TIMEOUT,(void*)1000,0);
    rc = SQLSetEnvAttr(henv,SQL_ATTR_LOGIN_TIMEOUT,(void*)QL_LOGIN_TIMEOUT_DEFAULT,0);
    连续三个SQLSetEnvAttr什么意思?
    呵呵。借楼主的光,学点东西了。希望楼主不要见怪。