连接数据库的时候我是根据买的一本参考书上教的写的,上面说数据库的连接在每个模块中都是必不可少的,所以将连接数据库的方法写在了程序的App类中。
我按上面的代码页连接到数据库了,也可以select数据了,但是写成公共类之后怎么用啊迷茫了,我在另一个对话框中要向数据库插入数据要怎么操作啊,调用什么函数吗?书上也没说,代码不会了 希望大神们详细讲一下。。谢谢了

解决方案 »

  1.   

    因为你把数据库的操作放在APP类了,这样就好办了,工程默认生成一个theApp变量,你直接用这个变量来调用操作数据库的函数及变量,就是这样:theApp.连接数据函数();theApp.插入数据函数();theApp.m_数据库变量;给你个例子:
    m_pConn->Open("Provider=SQLOLEDB.1;Server=PC-201012170947\\WINCC;DataBase=;UID=sa;PWD=","","",adConnectUnspecified);   
    这是SQL 2000的连接字符串,你把它加到下面的相应的语句里:#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
    头文件里类里:
    _ConnectionPtr m_pConnection;
    _RecordsetPtr m_pRecordset;
    _CommandPtr m_pCommand;CPP文件里:
    BOOL CDatabaseDll::OpenDatabaseFun(_ConnectionPtr pConnection,_RecordsetPtr pRecordset,_CommandPtr pCommand)
    {
    BOOL Result=TRUE;
    HRESULT hr;
    try   
    {   
    hr = pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
    if(SUCCEEDED(hr))   
    {   
    pConnection->ConnectionTimeout = 10;   
    // hr = m_pConnection->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\NW.mdb","", "", adModeUnknown);//这是OFFICE2003以下的
    hr = pConnection->Open("Provider=Microsoft.Ace.Oledb.12.0;Data Source=D:\\bagayalu.accdb;Jet OLEDB:Database Password=123456","", "", adModeUnknown);//这是OFFICE2007以上的,包括OFFICE2010,这个连接字符串就是在OFFICE2010下用的,没问题
        
    //m_pConnection->PutDefaultDatabase ((_bstr_t)"DB");//设置默认数据库   
        
    pCommand.CreateInstance(__uuidof(Command));
    pCommand->CommandTimeout = 5;
    pCommand->ActiveConnection = pConnection;
    }
    }
    catch(_com_error e)///捕捉异常
    {
    CString errormessage;
    errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    Result=FALSE;
    return Result;
    }   
    _variant_t var;
    float v1,v2,v3,v4;
    CString Re;
    pRecordset.CreateInstance(__uuidof(Recordset));
    try
    {
    pRecordset->Open("SELECT * FROM 表1",// 查询DemoTable表中所有字段   
    pConnection.GetInterfacePtr(),// 获取库接库的IDispatch指针   
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    while(!m_pRecordset->adoEOF)   
      {   
      var = m_pRecordset->GetCollect("v1");   
      if(var.vt != VT_NULL)
      v1=var.fltVal;
    // strName = (LPCSTR)_bstr_t(var);     var = m_pRecordset->GetCollect("v2");   
      if(var.vt != VT_NULL)   
      v2=var.fltVal;
      var = m_pRecordset->GetCollect("v3");   
      if(var.vt != VT_NULL)   
      v3=var.fltVal;
      var = m_pRecordset->GetCollect("v4");   
      if(var.vt != VT_NULL)   
      v4=var.fltVal;
      var = m_pRecordset->GetCollect("re");   
      if(var.vt != VT_NULL)
    Re = (LPCSTR)_bstr_t(var);
      m_pRecordset->MoveNext();   
      }   
    }
    catch(_com_error *e)
    {
    AfxMessageBox(e->ErrorMessage());
    Result=FALSE;
    return Result;
      

  2.   

    添加记录用_recordset->addNew();这样就增加了一条空记录;
    一般这么用
    _recordset->MoveLast();//移动到记录末尾
    _recordset->AddNew();//增加新记录
    _recordset->PutCollect(……)//然后是对新记录各字段的赋值,参数查一下SDK手册吧
    _recordset->Update(); //更新到数据库希望能帮到你。