用代码如何将DataGrid和ADO控件与Access数据库连接起来(连接后在DataGrid控件内可以看到数据库中的数据)
                                                               谢谢!!!

解决方案 »

  1.   

    转贴:ADO控件绑定数据库简易方法 作者:goldenshine 发表日期:2001-4-16 9:37:31
    假设在D:\\TEST\test.mdb中有一个表叫MATEARIAL; 在某一对话框上进行数据库的绑定的方法如下:
    1、 插入控件
    单击右键选择” Insert ActiveX Control”,在弹出的列表中选择”microsoft ADO Data Control”;
    用同样的方法选择”Microsoft Data Grid Control”;
    2、 设置控件
    打开第一个控件ADODC1的属性对话框进行设置,单击”Control->Build”,选择”Microsoft OLE DB Provider for ODBC Driver”, 单击next; 在use data source name下选择ms access 97 DataBase, 并在Enter the initial catlog to use下填入D:\test\test.mdb,此时可按TestConnection看看是否连接成功;如果成功,点确定后再次打开属性在RecordSource选项下Command Text(SQL)中填写MATERIAL,确认;
    打开第二个控件DataGrid1设置其属性,直接在ALL选项中DataSource选择IDC_ADODC1,确认;
    3、运行程序看看会怎样! 在DataGrid1里还有许多功能可设,如数据是否可加可改等,其它的一些功能要加代码去实现,我就不说了
    4、 请用ClassWizard给ADO Data 控件定义一个变量m_adodc; 以下的代码用来查找表中第一行第三列的一个值,设这个值为Cstring的;
    C_Recordset rcd=m_adodc.GetRecordset();
    rcd.MoveFirst();//第一行
    CFields flds=rcd.GetFields();CField fld=flds.GetItem(_variant_t((long)2));第三列
    CString a=(fld.GetValue()).bstrVal;//a就是你要找的第一行第三列的值
    并且:要在.cpp文件中插入以下语句:
    #include "COMDEF.H" //定义_variant_t的
    #include "field.h"
    #include "fields.h"
    #include "_recordset.h"也许你不会相信我所说的,因为按我的介绍ADO的使用太简单了,比别人的介绍都要简单,其实ADO的使用就这么简单;
      

  2.   

    SAMPLE: AdoDataGrid.exe Demonstrates How to Use ADO with DataGrid Control Using Visual C++ Q229029
    --------------------------------------------------------------------------------
    The information in this article applies to:ActiveX Data Objects (ADO), versions 2.0, 2.1, 2.5, 2.6 
    Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
    http://download.microsoft.com/download/ado/Utility/1.0/WIN98/EN-US/AdoDataGrid.exe