如何在C ++中使用ms access数据库? [英] How to use ms access database in C++?

查看:82
本文介绍了如何在C ++中使用ms access数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在win32中将数据库链接到我的项目代码。昨天我看到链接

https://msdn.microsoft.com/en-us/library/office /ff965871(v=office.14)#DataProgrammingWithAccess2010_ATLOLEDBExample

但是没有使用上面的代码。请帮帮我?
提前付款

我尝试了什么:

我在上面尝试过链接不同的数据库,但没有工作。





此链接也不起作用



https://msdn.microsoft.com/en-us/library/cc811599.aspx#Y3869



[由Jochen Arndt编辑:插入的代码和文字作为解决方案发布]

 #导入C:\Program Files \Common Files \System\ADO\msado15.dll\ 
no_namespace重命名( EOF EndOfFile

hr = S_OK;
尝试
{
CoInitialize(NULL);
_bstr_t strCnn( Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\\ \\Sankar\\Database1.accdb);
_RecordsetPtr pRstAuthors = NULL;

_ConnectionPtr pConnection = NULL;

// 调用Create实例来实例化记录集
hr = pRstAuthors.CreateInstance( __ uuidof (Recordset));
pConnection.CreateInstance(_uuidof(Connection));
pConnection-> ConnectionString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E: \\Sankar\\Database1.accdb;
pConnection-> ConnectionTimeout = 30 ;
if (FAILED(hr))
{
// printf(无法创建记录集实例\ n);
return ;
}
// 打开记录集以获取客户表中的记录
pRstAuthors-> Open( SELECT * FROM Customers Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\Sankar \\Database1.accdb,adOpenStatic,adLockReadOnly,adCmdText);

_bstr_t valField1;
int valField2;
pRstAuthors-> MoveFirst();
if (!pRstAuthors-> EndOfFile)
{
while (!pRstAuthors-> EndOfFile)
{
valField1 = pRstAuthors-> Fields-> GetItem( 用户名) - > Value.intVal;
}
}
}
catch (_ com_error& ce)
{
< span class =code-comment> //
sprintf(错误:%s \ n,ce.Description);
}
CoUninitialize();



pRstAuthors->打开:在这种方法中我遇到了错误。

[/ EDIT]

解决方案

参见此文章:使用Native C或C ++开发Access 2007解决方案 [ ^ ]

有关Provider = Microsoft.ACE.OLEDB.12的详细信息,请参阅此处: Microsoft ACE OLEDB 12.0连接字符串 - ConnectionStrings.com [ ^ ]



示例:

 _ bstr_t strCnn(  Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\Sankar \\Database1.accdb; Persist Security Info = False;); 


这些都包含在您问题的链接中。



您必须决定使用哪种方法:


  • 直接DAO(Win32)

  • ATL OLE DB(ATL / MFC)

  • ADO(Win32)

  • 直接ODBC(Win32)

  • MFC ODBC(MFC)



Win32方法需要导入DLL,这可能有点难以避免名称冲突。



根据使用的方法,可能需要随应用程序一起提供其他软件包,以确保在客户端上安装使用过的数据库接口(Microsoft Access Redistributables或MDAC应该与所有re一起提供)从Vista / 7开始的Windows版本。



另请参阅有关32/64位的说明。如果可能的话,构建一个32位的应用程序。



DAO提供最好的支持(比其他方法更多的功能)。使用MFC时,有OLE DB和ODBC类可以减少要写入的代码量。



如果您遇到特定方法,请再次询问或更新您的问题有一个简短的代码示例和详细的问题描述。 不工作对我们没有任何意义;特别是没有一段代码和错误信息时。





如果函数返回某种状态/错误,检查并立即报告错误。您的代码段不会对所有函数执行此操作,并且有时在调用其他函数后报告错误。另请阅读已使用函数的文档,以了解必须传递哪些参数。



您还必须遵循有关要调用的函数顺序的示例:


  • 创建_ConnectionPtr

  • 打开连接

  • 创建RecordsetPtr

  • 打开通过连接的记录集



详细:

 pRstAuthors-> Open(SELECT * FROM Customers,Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\Sankar \\ \\\Database1.accdb,adOpenStatic,adLockReadOnly,adCmdText); 

不是有效的呼叫。它错过了通过连接。它必须是:

 pRstAuthors-> Open(SELECT * FROM Customers,Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\\ \\ Sankar \\Database1.accdb,
_variant_t((IDispatch *)pConnection,true),
adOpenStatic,
adLockReadOnly,
adCmdText);

[/编辑]


How to link database to my project code in win32.Yesterday i saw below link 

https://msdn.microsoft.com/en-us/library/office/ff965871(v=office.14)#DataProgrammingWithAccess2010_ATLOLEDBExample 

but no use above code.Please help me?
Thanks in advance

What I have tried:

I tried above link with different databases but not work.



This link also not work

https://msdn.microsoft.com/en-us/library/cc811599.aspx#Y3869

[EDIT by Jochen Arndt: Inserted code and text posted as solution]

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

hr=S_OK;
try
{
    CoInitialize(NULL);
    _bstr_t strCnn("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb");
    _RecordsetPtr pRstAuthors=NULL;
     
    _ConnectionPtr  pConnection=NULL;
     
    // Call Create instance to instantiate the Record set
    hr = pRstAuthors.CreateInstance(__uuidof(Recordset));
    pConnection.CreateInstance(_uuidof(Connection));
    pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb";
    pConnection->ConnectionTimeout = 30;  
    if(FAILED(hr))
    {
        // printf("Failed creating record set instance\n");
        return;
    }
    //Open the Record set for getting records from Customer table
    pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",adOpenStatic,adLockReadOnly, adCmdText);
 
    _bstr_t valField1;
    int valField2;
    pRstAuthors->MoveFirst();
    if (!pRstAuthors->EndOfFile)
    {
        while(!pRstAuthors->EndOfFile)
        {
            valField1 = pRstAuthors->Fields->GetItem("username")->Value.intVal;
        }
    }
}
catch(_com_error & ce)
{
    //sprintf("Error:%s\n",ce.Description);
}
CoUninitialize();


pRstAuthors->Open: In this method i got error.
[/EDIT]

解决方案

See this article: Developing Access 2007 Solutions with Native C or C++[^]
See here for connection details with Provider=Microsoft.ACE.OLEDB.12 : Microsoft ACE OLEDB 12.0 Connection Strings - ConnectionStrings.com[^]

Example:

_bstr_t strCnn("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb;Persist Security Info=False;");


It is all contained in the link from your question.

You have to decide which method you want to use:

  • Direct DAO (Win32)
  • ATL OLE DB (ATL/MFC)
  • ADO (Win32)
  • Direct ODBC (Win32)
  • MFC ODBC (MFC)

The Win32 methods require importing of a DLL which might be a bit tricky to avoid name conflicts.

Depending on the used method it might be necessary to ship additional packages with your application to ensure that the used database interface is installed on the client (Microsoft Access Redistributables or MDAC which should be present with all recent Windows versions since Vista/7).

See also the notes about 32/64 bit. If possible build a 32-bit application.

DAO provides the best support (more functions than the other methods). When using MFC, there are classes for OLE DB and ODBC which reduces the amount of code to write.

If you got stuck with a specific method, ask again or update your question with a short code example and a detailed problem description. "Not work" does not mean anything to us; especially when not having a single piece of code and an error message.

[EDIT: For code posted meanwhile]
If a function returns some kind of status / error, check that and report errors immediately. Your code snippet does not do that for all functions and reports errors sometimes after calling other function meanwhile. Read also the documentation for the used function to know which parameters has to be passed.

You have also to follow the examples regarding the order of the functions to be called:

  • Create a _ConnectionPtr
  • Open the connection
  • Create a RecordsetPtr
  • Open the recordset passing the connection

In detail:

pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",adOpenStatic,adLockReadOnly, adCmdText);

is not a valid call. It misses passing the connection. It must be:

pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",
_variant_t((IDispatch *) pConnection, true),
adOpenStatic,
adLockReadOnly, 
adCmdText);

[/EDIT]


这篇关于如何在C ++中使用ms access数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆