工作需要,某从cb转到vc来了,初次使用,顿感不便。有如下几点不明:
首先字符串在vc中怎么处理的。
其次如何用ado连接数据库。
最后再请大家提点建议。

解决方案 »

  1.   

    www.vckbase.com
    里面有好多vc资料!
      

  2.   

    CString s;
    // 加载ADO库
    #import "C:\program files\common files\system\ado\msado15.dll" no_namespace \
             rename("EOF", "adoEOF")
    _ConnectionPtr pMyConn; // Connection对象
    pMyConn.CreateInstance(__uuidof(Connection));
    if(pMyConn->GetState() == adStateClosed)
    {
    try
    {
    //pMyConn.CreateInstance(__uuidof(Connection));
    // 连接数据库
    if (SUCCEEDED(pMyConn->Open(m_strDSN.AllocSysString(),"","",-1)))
    {
    return TRUE;
    }
    }
    catch(_com_error e)
    {
    //CString strMsg;
    //strMsg.LoadString(DB_CONNECT_DATA_ERROR);
    MessageBox(NULL,m_strDSN,e.ErrorMessage(),MB_OK|MB_ICONSTOP);
    return FALSE;
    }
    }
      

  3.   

    字符串有两类:
    CString VC的字符串类
    string in STL
      

  4.   

    ADO例程太多了,随便搜一下都是。
    先给一个最简单的给你吧:首先需要在APP类的InitInstance()函数的开头处添加如下代码:
    // 初始化COM库,以便于ADO进行数据库操作。
    AfxOleInit();
    此句代码一般放在AfxEnableControlContainer()语句之后。然后在Stdafx.h中添加如下代码:
    #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
    此句代码应放在所有include语句之后。接着在需要的地方添加相应代码就可以了:
    // 建立连接
    _ConnectionPtr m_pConnection = NULL;

    try
    {
    hr = m_pConnection.CreateInstance("ADODB.Connection"); //创建Connection对象 if (SUCCEEDED(hr))
    {
    hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\MyDB.mdb","","",adModeUnknown); // 连接数据库 if (SUCCEEDED(hr)) 
    AfxMessageBox("Open succeeded!");
    else
    AfxMessageBox("Open failed!");
    }
    else
    AfxMessageBox("Connection failed!");
    }
    catch(_com_error e)
    {
    CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);
    }
    // 打开记录集 _RecordsetPtr m_pRecordset; if(FAILED(m_pRecordset.CreateInstance( "ADODB.Recordset")))
    {
    //m_pDoc->m_initialized=FALSE;
    AfxMessageBox("Recordset failed");
    return false;
    }

    try{
    m_pRecordset->Open(_variant_t("表1"),
                _variant_t((IDispatch *)m_pConnection,true), adOpenKeyset,
                adLockOptimistic, adCmdTable);
    }
    catch (_com_error &e)
    {
    CString errormessage; errormessage.Format("不能打开表1!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);
    } m_pRecordset->MoveFirst();    CString Product; long Price; char chh[4]; // 读取数据
    while (m_pRecordset->adoEOF == VARIANT_FALSE) // 判断是否到数据库记录集的末尾。
    {

    Product = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem
    (_variant_t("Product"))->Value);     // Product是MyDB数据库的表1中的项名。

    AfxMessageBox(Product);

    Price = (long)(m_pRecordset->Fields->GetItem
    (_variant_t("Price"))->Value);

    _ltoa(Price, chh, 10);

    AfxMessageBox(chh);

    m_pRecordset->MoveNext();

    } // 修改数据
    m_pRecordset->MoveFirst(); m_pRecordset->Fields->GetItem(_variant_t("Product"))->Value = _bstr_t("Siemens SL55"); m_pRecordset->Fields->GetItem(_variant_t("Price"))->Value = (long)2850; m_pRecordset->Update(); // 添加新记录
    if (!m_pRecordset->Supports(adAddNew))
    {
    AfxMessageBox("This DB doesn't support AddNew");
    return false;
    }    m_pRecordset->AddNew(); m_pRecordset->Fields->GetItem(_variant_t("Product"))->Value = _bstr_t("Canon"); m_pRecordset->Fields->GetItem(_variant_t("Price"))->Value = (long)3100; m_pRecordset->Fields->GetItem(_variant_t("Num"))->Value = (long)11; m_pRecordset->Update(); // 删除记录
    m_pRecordset->MoveLast(); m_pRecordset->Delete(adAffectCurrent); m_pRecordset->Update(); // 搜索满足条件的记录
    m_pRecordset->MoveFirst(); m_pRecordset->Find("Price > 3000", 0, adSearchForward, ""); // 显示搜索结果
    if (m_pRecordset->adoEOF == VARIANT_FALSE)
    {
    Product = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem
    (_variant_t("Product"))->Value);     

    AfxMessageBox(Product);

    Price = (long)(m_pRecordset->Fields->GetItem
    (_variant_t("Price"))->Value);

    _ltoa(Price, chh, 10);

    AfxMessageBox(chh);

    m_pRecordset->MoveFirst();
    } // 执行SQL语句,将Price字段值加100,添加一个记录,统计记录数。
    _variant_t RecordsAffected; m_pConnection->Execute("UPDATE 表1 SET Price = Price + 100", &RecordsAffected, adCmdText); m_pConnection->Execute("INSERT INTO 表1 (Product,Price,Num) VALUES ('Fuji', 2900, 2)", &RecordsAffected, adCmdText); m_pRecordset = m_pConnection->Execute("SELECT COUNT(*) FROM 表1", &RecordsAffected, adCmdText); _variant_t vIndex = (long)0; _variant_t vCount = m_pRecordset->GetCollect(vIndex); CString str; str.Format("共有%d条记录", vCount.lVal); AfxMessageBox(str); // 关闭记录集
    m_pRecordset->Close(); m_pRecordset.Release(); // 断开数据库连接    m_pConnection->Close();

    m_pConnection.Release();
      

  5.   

    学BCB能把人学废了,TNND我原来学VC,听说BCB简单又学BCB,现在又回来了。BCB不爽
      

  6.   

    欢迎欢迎 我添两句
    m_pRecordset->Open(_variant_t("表1"),
                _variant_t((IDispatch *)m_pConnection,true), adOpenKeyset,
                adLockOptimistic, adCmdTable);
    打开一个recordset的时候 第一个参数可以传sql 直接传CString就可以 传进去的时候强制转换成_variant_t类型的就可以了(当然传sql的话最后的参数要改成adCmdTable)
    第二个参数传的是连接对象 一般例子都象上面那样写 有一个比较简单的传参方法
    把_variant_t((IDispatch *)m_pConnection改为
    m_pConnnection.GetInterfacePtr()
    取值的时候m_pRs->GetFields()->GetItem("fieldname")->Value;
    返回的都是一个_variant_t 就是VARIANT这个结构的一个封装
    刚开始用着可能别扭 用习惯了就好了 反而会觉得用这个封装了一个大union的struct来传递数据会比较保险
    一般如果取char类型的字段 直接把Value.bstrVal赋值给一个CString就可以了 CString的operator = 会帮你完成剩下的事情
    判断一个Value的类型 用Value.vt就可以 具体的类型在sdk中有宏定义 比如VT_INT VT_BSTR等等
      

  7.   

    欢迎欢迎 我添两句
    m_pRecordset->Open(_variant_t("表1"),
                _variant_t((IDispatch *)m_pConnection,true), adOpenKeyset,
                adLockOptimistic, adCmdTable);
    打开一个recordset的时候 第一个参数可以传sql 直接传CString就可以 传进去的时候强制转换成_variant_t类型的就可以了(当然传sql的话最后的参数要改成adCmdTable)
    第二个参数传的是连接对象 一般例子都象上面那样写 有一个比较简单的传参方法
    把_variant_t((IDispatch *)m_pConnection改为
    m_pConnnection.GetInterfacePtr()
    取值的时候m_pRs->GetFields()->GetItem("fieldname")->Value;
    返回的都是一个_variant_t 就是VARIANT这个结构的一个封装
    刚开始用着可能别扭 用习惯了就好了 反而会觉得用这个封装了一个大union的struct来传递数据会比较保险
    一般如果取char类型的字段 直接把Value.bstrVal赋值给一个CString就可以了 CString的operator = 会帮你完成剩下的事情
    判断一个Value的类型 用Value.vt就可以 具体的类型在sdk中有宏定义 比如VT_INT VT_BSTR等等
      

  8.   

    写错了 ^^
    (当然传sql的话最后的参数要改成adCmdTable)
    ->
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^adCmdText
      

  9.   

    我来也,原来我也是学vc,学了近两个月也就只是会用用向导而以,几乎是一无所获啊,
    后来工作了,用bcb感觉很方便,大约半年后用delphi,更是如鱼得水啊。
    最近看com本质论,突然觉得自己废了,郁闷非常。
    c++真好,vc又更近于本质。
    学cb是聪明的选择,学vc是必然的选择。