作者在 2009-04-12 09:10:11 发布以下内容
				存储过程为:
 create procedure [dbo].[ps_show1]
create procedure [dbo].[ps_show1]
 @maxID int
    @maxID int
 as
as
 select * from ImportBatch
select * from ImportBatch
 where BatchID <= @maxID
where BatchID <= @maxID
存储过程要求输入一个参数,同时返回一个记录集
下面是应用程序中访问存储过程的关键代码:
 _RecordsetPtr m_pRecordSetTemp = NULL;
_RecordsetPtr m_pRecordSetTemp = NULL;
 _ConnectionPtr m_pConnection = NULL;
    _ConnectionPtr m_pConnection = NULL;
 _CommandPtr  m_pCommand = NULL;
    _CommandPtr  m_pCommand = NULL;
 
   
 _bstr_t strCon(
    _bstr_t strCon(
 "Provider=SQLOLEDB.1;Data Source=1.1.1.1;Initial Catalog=yourdbname;User ID=sa;Password=123456;"
        "Provider=SQLOLEDB.1;Data Source=1.1.1.1;Initial Catalog=yourdbname;User ID=sa;Password=123456;"
 );
        );
 
   
 CoInitialize(NULL);
    CoInitialize(NULL);
 try
    try

 ...{
    ...{   
 HRESULT hr = m_pConnection.CreateInstance((__uuidof(Connection)));
        HRESULT hr = m_pConnection.CreateInstance((__uuidof(Connection)));
 if(FAILED(hr)) return;
        if(FAILED(hr)) return;
 
       
 //Open the SQL Server connection
        //Open the SQL Server connection
 m_pConnection->PutCursorLocation(adUseClient);
        m_pConnection->PutCursorLocation(adUseClient);
 hr = m_pConnection->Open(strCon,"","",0);
        hr = m_pConnection->Open(strCon,"","",0);
 if(FAILED(hr)) return;
        if(FAILED(hr)) return;
 
       
 //Create the Connection pointer
        //Create the Connection pointer
 m_pCommand.CreateInstance(__uuidof(Command));
        m_pCommand.CreateInstance(__uuidof(Command));
 
       
 ASSERT(m_pCommand != NULL);
        ASSERT(m_pCommand != NULL);

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

 m_pCommand->Parameters->Append(pParamMember1);
        m_pCommand->Parameters->Append(pParamMember1);
 
               
 //执行存储过程
        //执行存储过程
 m_pCommand->ActiveConnection = m_pConnection;
        m_pCommand->ActiveConnection = m_pConnection;
 m_pCommand->CommandText="dbo.ps_show1";  //存储过程名称
        m_pCommand->CommandText="dbo.ps_show1";  //存储过程名称
 m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
        m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
 
       
 m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
        m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
 if(m_pRecordSetTemp)
        if(m_pRecordSetTemp)

 ...{
        ...{
 long recordcount;
            long recordcount;
 recordcount = m_pRecordSetTemp->RecordCount;
            recordcount = m_pRecordSetTemp->RecordCount;
 while(!(m_pRecordSetTemp->EndOfFile))
            while(!(m_pRecordSetTemp->EndOfFile))

 ...{
            ...{
 CString msg;
                CString msg;
 msg.Format("记录号:%s-卡总数:%s-说明:%s ",
                msg.Format("记录号:%s-卡总数:%s-说明:%s ",
 (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("BatchID"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("BatchID"),
 (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Cards_amount"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Cards_amount"),
 (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Note"));
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Note"));
 
               
 m_pRecordSetTemp->MoveNext();
                m_pRecordSetTemp->MoveNext();
 AfxMessageBox(msg);
                AfxMessageBox(msg);
 }
            }
 }
        }
 
       
 }
    }
 catch(_com_error e)
    catch(_com_error e)

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

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

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

 /**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
需要注意的问题:
1、_ParameterPtr 属性设置要全,不能漏掉一些属性。默认属性可能导致工作不正常。
2、m_pConnection->PutCursorLocation(adUseClient);游标设置要正确,否则m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);返回的记录集 的m_pRecordSetTemp->RecordCount属性为不可访问状态(-1);
				 create procedure [dbo].[ps_show1]
create procedure [dbo].[ps_show1] @maxID int
    @maxID int as
as select * from ImportBatch
select * from ImportBatch where BatchID <= @maxID
where BatchID <= @maxID存储过程要求输入一个参数,同时返回一个记录集
下面是应用程序中访问存储过程的关键代码:
 _RecordsetPtr m_pRecordSetTemp = NULL;
_RecordsetPtr m_pRecordSetTemp = NULL; _ConnectionPtr m_pConnection = NULL;
    _ConnectionPtr m_pConnection = NULL; _CommandPtr  m_pCommand = NULL;
    _CommandPtr  m_pCommand = NULL; 
    _bstr_t strCon(
    _bstr_t strCon( "Provider=SQLOLEDB.1;Data Source=1.1.1.1;Initial Catalog=yourdbname;User ID=sa;Password=123456;"
        "Provider=SQLOLEDB.1;Data Source=1.1.1.1;Initial Catalog=yourdbname;User ID=sa;Password=123456;" );
        ); 
    CoInitialize(NULL);
    CoInitialize(NULL); try
    try
 ...{
    ...{    HRESULT hr = m_pConnection.CreateInstance((__uuidof(Connection)));
        HRESULT hr = m_pConnection.CreateInstance((__uuidof(Connection))); if(FAILED(hr)) return;
        if(FAILED(hr)) return; 
        //Open the SQL Server connection
        //Open the SQL Server connection m_pConnection->PutCursorLocation(adUseClient);
        m_pConnection->PutCursorLocation(adUseClient); hr = m_pConnection->Open(strCon,"","",0);
        hr = m_pConnection->Open(strCon,"","",0); if(FAILED(hr)) return;
        if(FAILED(hr)) return; 
        //Create the Connection pointer
        //Create the Connection pointer m_pCommand.CreateInstance(__uuidof(Command));
        m_pCommand.CreateInstance(__uuidof(Command)); 
        ASSERT(m_pCommand != NULL);
        ASSERT(m_pCommand != NULL);
 //输入参数 Member
        //输入参数 Member _ParameterPtr pParamMember1;
        _ParameterPtr pParamMember1; pParamMember1.CreateInstance("ADODB.Parameter");
        pParamMember1.CreateInstance("ADODB.Parameter"); pParamMember1->Name="maxID";  //所用存储过程参数名称
        pParamMember1->Name="maxID";  //所用存储过程参数名称 pParamMember1->Type=adChar; //参数类型
        pParamMember1->Type=adChar; //参数类型 pParamMember1->Size=32;  //参数大小
        pParamMember1->Size=32;  //参数大小 pParamMember1->Direction=adParamInput;//表明是输入参数
        pParamMember1->Direction=adParamInput;//表明是输入参数 pParamMember1->Value=_variant_t(2L);//参数值
        pParamMember1->Value=_variant_t(2L);//参数值
 m_pCommand->Parameters->Append(pParamMember1);
        m_pCommand->Parameters->Append(pParamMember1); 
                //执行存储过程
        //执行存储过程 m_pCommand->ActiveConnection = m_pConnection;
        m_pCommand->ActiveConnection = m_pConnection; m_pCommand->CommandText="dbo.ps_show1";  //存储过程名称
        m_pCommand->CommandText="dbo.ps_show1";  //存储过程名称 m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
        m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc 
        m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
        m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc); if(m_pRecordSetTemp)
        if(m_pRecordSetTemp)
 ...{
        ...{ long recordcount;
            long recordcount; recordcount = m_pRecordSetTemp->RecordCount;
            recordcount = m_pRecordSetTemp->RecordCount; while(!(m_pRecordSetTemp->EndOfFile))
            while(!(m_pRecordSetTemp->EndOfFile))
 ...{
            ...{ CString msg;
                CString msg; msg.Format("记录号:%s-卡总数:%s-说明:%s ",
                msg.Format("记录号:%s-卡总数:%s-说明:%s ", (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("BatchID"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("BatchID"), (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Cards_amount"),
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Cards_amount"), (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Note"));
                    (LPCTSTR)(_bstr_t)m_pRecordSetTemp->GetCollect("Note")); 
                m_pRecordSetTemp->MoveNext();
                m_pRecordSetTemp->MoveNext(); AfxMessageBox(msg);
                AfxMessageBox(msg); }
            } }
        } 
        }
    } catch(_com_error e)
    catch(_com_error e)
 ...{
    ...{ CString temp;
        CString temp; temp.Format(_T("Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d "), e.ErrorMessage(), __FILE__, __LINE__);
        temp.Format(_T("Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d "), e.ErrorMessage(), __FILE__, __LINE__); AfxMessageBox(temp);
        AfxMessageBox(temp); }
    } catch (...)
    catch (...)
 ...{
    ...{ AfxMessageBox("未知错误!");
        AfxMessageBox("未知错误!"); }
    } 
   
 /**//*
    /**//* m_pRecordSetTemp->Release();
    m_pRecordSetTemp->Release(); m_pRecordSetTemp->Close();
    m_pRecordSetTemp->Close(); m_pCommand->Release();
    m_pCommand->Release(); 
    m_pConnection->Release();
    m_pConnection->Release(); m_pConnection->Close();
    m_pConnection->Close(); */
    */ CoUninitialize();
    CoUninitialize();
 /**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////需要注意的问题:
1、_ParameterPtr 属性设置要全,不能漏掉一些属性。默认属性可能导致工作不正常。
2、m_pConnection->PutCursorLocation(adUseClient);游标设置要正确,否则m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);返回的记录集 的m_pRecordSetTemp->RecordCount属性为不可访问状态(-1);