简体   繁体   English

使用 C++ 将数据连接并插入到 MS Access 表中

[英]Connect and Insert data into a MS Access table using C++

I am working on a project that requires me to perform insert query on an MS Access table.我正在开发一个需要我对 MS Access 表执行插入查询的项目。 I have been searching everywhere online but nothing seems to work.我一直在网上到处搜索,但似乎没有任何效果。 Any help would be greatly appreciated.任何帮助将不胜感激。 Also, I have to write this for VS2008 and and Visual C++ 6.0.另外,我必须为 VS2008 和 Visual C++ 6.0 编写这个。 Thanks谢谢

You have (far too) many choices: ADO/RDO , DAO , ODBC , and OLE DB , to name only a few.您有(太多)选择: ADO/RDODAOODBCOLE DB ,仅举几例。 DAO is officially deprecated. DAO 已被正式弃用。 ADO/RDO aren't officially, but MS doesn't seem to care much about them anymore either. ADO/RDO 不是正式的,但 MS 似乎也不再关心它们了。 With VC 6, however, obsolescent is pretty much a fact of life.然而,在 VC 6 中,过时已成为现实。 I believe they're all still supported to at least some degree under VS 2008, but they no longer, for example, include any wizards to help with using DAO.我相信它们在 VS 2008 下至少在某种程度上仍然受到支持,但它们不再包括任何向导来帮助使用 DAO。

That basically leaves OLE DB and ODBC as your first couple of choices.这基本上使 OLE DB 和 ODBC 成为您的首选。 MS is still actively supporting and developing them, and the others are unlikely to provide any major advantage anyway. MS 仍在积极支持和开发它们,而其他的无论如何都不太可能提供任何主要优势。

I should add that VC++ 6.0 provides quite a few features missing from VS2008 for writing applications that use databases.我应该补充一点,VC++ 6.0 提供了很多 VS2008 中缺少的用于编写使用数据库的应用程序的功能。 You might want to look at what I showed in a previous question for some guidance.您可能想查看我在上一个问题中展示的内容以获得一些指导。

Use ODBC .使用ODBC Example of connecting to database and executing INSERT query:连接数据库并执行 INSERT 查询的示例:

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <sqlext.h>

WCHAR szDSN[] = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\users.mdb";

int _tmain(int argc, _TCHAR* argv[])
{    

HENV    hEnv;
HDBC    hDbc;

/* ODBC API return status */
RETCODE rc;

int     iConnStrLength2Ptr;
WCHAR    szConnStrOut[256];

WCHAR* query = L"INSERT INTO [Users] (name,surname) VALUES ('John','Smith');";

HSTMT           hStmt;

/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);

/* Connect to the database */
rc = SQLDriverConnect(hDbc, NULL, (WCHAR*)szDSN, 
    SQL_NTS, (WCHAR*)szConnStrOut, 
    255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc)) 
{

    wprintf(L"Successfully connected to database. Data source name: \n  %s\n", 
        szConnStrOut);  

    /* Prepare SQL query */
    wprintf(L"SQL query:\n  %s\n", query);

    rc = SQLAllocStmt(hDbc,&hStmt);
    rc = SQLPrepare(hStmt, query, SQL_NTS);   

    /* Excecute the query */
    rc = SQLExecute(hStmt); 
    if (SQL_SUCCEEDED(rc)) 
    {
        wprintf(L"SQL Success\n");
    }
    else{
        wprintf(L"SQL Failed\n");
    }
}
else
{
    wprintf(L"Couldn't connect to %s.\n",szDSN);
}

/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

getchar();
return 0;
}

Source: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc811599(v=office.12)资料来源: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc811599(v=office.12)

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

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