VC中用ADO访问SqlServer中的存储过程
来源:互联网 发布:javascript模式下载 编辑:程序博客网 时间:2024/05/16 11:34
最近想把项目中的数据库访问、操作方式改为存储过程,无奈原项目是用CV6.0开发的,对存储过程支持不是很好,只有自己用直接用ADO来干。~~~想想BC、Delphi,人家还是有好处的~~~
存储过程为:
存储过程要求输入一个参数,同时返回一个记录集
下面是应用程序中访问存储过程的关键代码:
需要注意的问题:
1、_ParameterPtr 属性设置要全,不能漏掉一些属性。默认属性可能导致工作不正常。
2、m_pConnection->PutCursorLocation(adUseClient);游标设置要正确,否则m_pRecordSetTemp = m_pCommand->Execute(NULL, NULL, adCmdStoredProc);返回的记录集 的m_pRecordSetTemp->RecordCount属性为不可访问状态(-1);关于游标
存储过程为:
create procedure [dbo].[ps_show1]
@maxID int
as
select * from ImportBatch
where BatchID <= @maxID
@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();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_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);关于游标
- VC中用ADO访问SqlServer中的存储过程
- SQLServer存储过程和ADO.NET访问存储过程-整理
- 使用VC在ADO中访问存储过程的方法
- vc ado调用存储过程
- ADO中sqlserver存储过程使用
- ADO中sqlserver存储过程使用
- 网络工作室暑假后第二次培训资料(SQLServer存储过程和ADO.NET访问存储过程)整理(一)
- 网络工作室暑假后第二次培训资料(SQLServer存储过程和ADO.NET访问存储过程)整理(二)
- VC中使用ADO调用存储过程
- VC中使用ADO调用存储过程
- VC++ ADO调用存储过程方法
- VC++ ADO调用存储过程方法
- ADO.NET使用存储过程访问数据库
- SQLserver存储过程中的事务
- C# 访问sqlserver 存储过程小试牛刀
- SQLServer中用存储过程去除表中重复客户信息
- sqlserver中用存储过程创建sql任务(job)
- SQLServer访问ADO
- oracle实现自动插入自增长序列
- 大家好!很久没发资源了!
- 在jboss上部署web应用
- 淘宝开店8大绝招(转)
- VC6的多文档问题
- VC中用ADO访问SqlServer中的存储过程
- Request.ServerVariables参数集
- 十二年磨一剑
- Assembly信息类
- html中嵌入flv格式文件的代码
- VB中动态创建控件
- C#中判断字符是否为中文
- 浅议VB6字节数组和字符串的相互转换
- 一篇好文,以在迷茫时阅读