VC中用ADO访问SqlServer中的存储过程

来源:互联网 发布:javascript模式下载 编辑:程序博客网 时间:2024/05/16 11:34
最近想把项目中的数据库访问、操作方式改为存储过程,无奈原项目是用CV6.0开发的,对存储过程支持不是很好,只有自己用直接用ADO来干。~~~想想BC、Delphi,人家还是有好处的~~~
存储过程为:
create procedure [dbo].[ps_show1]
    
@maxID int
as
select * from ImportBatch
where BatchID <= @maxID

存储过程要求输入一个参数,同时返回一个记录集

下面是应用程序中访问存储过程的关键代码:
_RecordsetPtr m_pRecordSetTemp = NULL;
    _ConnectionPtr m_pConnection 
= NULL;
    _CommandPtr  m_pCommand 
= NULL;
   
    _bstr_t strCon(
        
"Provider=SQLOLEDB.1;Data Source=1.1.1.1;Initial Catalog=yourdbname;User ID=sa;Password=123456;"
        );
   
    CoInitialize(NULL);
    
try
    
{   
        HRESULT hr 
= m_pConnection.CreateInstance((__uuidof(Connection)));
        
if(FAILED(hr)) return;
       
        
//Open the SQL Server connection
        m_pConnection->PutCursorLocation(adUseClient);
        hr 
= m_pConnection->Open(strCon,"","",0);
        
if(FAILED(hr)) return;
       
        
//Create the Connection pointer
        m_pCommand.CreateInstance(__uuidof(Command));
       
        ASSERT(m_pCommand 
!= NULL);

        
//输入参数 Member
        _ParameterPtr pParamMember1;
        pParamMember1.CreateInstance(
"ADODB.Parameter");
        pParamMember1
->Name="maxID";  //所用存储过程参数名称
        pParamMember1->Type=adChar; //参数类型
        pParamMember1->Size=32;  //参数大小
        pParamMember1->Direction=adParamInput;//表明是输入参数
        pParamMember1->Value=_variant_t(2L);//参数值

        m_pCommand
->Parameters->Append(pParamMember1);
               
        
//执行存储过程
        m_pCommand->ActiveConnection = m_pConnection;
        m_pCommand
->CommandText="dbo.ps_show1";  //存储过程名称
        m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
       
        m_pRecordSetTemp 
= m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
        
if(m_pRecordSetTemp)
        
{
            
long recordcount;
            recordcount 
= m_pRecordSetTemp->RecordCount;
            
while(!(m_pRecordSetTemp->EndOfFile))
            
{
                CString msg;
                msg.Format(
"记录号:%s-卡总数:%s-说明:%s ",
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp
->GetCollect("BatchID"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp
->GetCollect("Cards_amount"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp
->GetCollect("Note"));
               
                m_pRecordSetTemp
->MoveNext();
                AfxMessageBox(msg);
            }

        }

       
    }

    
catch(_com_error e)
    
{
        CString temp;
        temp.Format(_T(
"Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d "), e.ErrorMessage(), __FILE__, __LINE__);
        AfxMessageBox(temp);
    }

    
catch (...)
    
{
        AfxMessageBox(
"未知错误!");
    }

   
    
/*
    m_pRecordSetTemp->Release();
    m_pRecordSetTemp->Close();
    m_pCommand->Release();
   
    m_pConnection->Release();
    m_pConnection->Close();
    
*/

    CoUninitialize();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

需要注意的问题:
1、_ParameterPtr 属性设置要全,不能漏掉一些属性。默认属性可能导致工作不正常。
2、m_pConnection->PutCursorLocation(adUseClient);游标设置要正确,否则m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);返回的记录集 的m_pRecordSetTemp->RecordCount属性为不可访问状态(-1);关于游标
原创粉丝点击