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

发布于 2024-11-17 16:29:40 字数 111 浏览 3 评论 0原文

我正在开发一个项目,要求我对 MS Access 表执行插入查询。我一直在网上到处搜索,但似乎没有任何效果。任何帮助将不胜感激。另外,我必须为 VS2008 和 Visual C++ 6.0 编写此内容。谢谢

I am working on a project that requires me to perform insert query on an MS Access table. 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. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-11-24 16:29:40

使用 ODBC 。连接到数据库并执行 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;
}

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

Use ODBC. Example of connecting to database and executing INSERT query:

#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://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc811599(v=office.12)

路弥 2024-11-24 16:29:40

您有(太多)选择: ADO/ RDODAOODBCOLE DB,仅举几例。 DAO 已被正式弃用。 ADO/RDO 并不是正式的,但 MS 似乎也不再关心它们了。然而,对于 VC 6,过时几乎是不争的事实。我相信 VS 2008 至少在某种程度上仍然支持它们,但它们不再包含任何帮助使用 DAO 的向导。

这基本上让 OLE DB 和 ODBC 作为您的第一选择。 MS 仍在积极支持和开发它们,其他的无论如何也不可能提供任何重大优势。

我应该补充一点,VC++ 6.0 提供了 VS2008 所缺少的许多功能来编写使用数据库的应用程序。您可能想看看我在 上一个问题以获得一些指导。

You have (far too) many choices: ADO/RDO, DAO, ODBC, and OLE DB, to name only a few. DAO is officially deprecated. ADO/RDO aren't officially, but MS doesn't seem to care much about them anymore either. With VC 6, however, obsolescent is pretty much a fact of life. 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.

That basically leaves OLE DB and ODBC as your first couple of choices. MS is still actively supporting and developing them, and the others are unlikely to provide any major advantage anyway.

I should add that VC++ 6.0 provides quite a few features missing from VS2008 for writing applications that use databases. You might want to look at what I showed in a previous question for some guidance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文