关于MFC中的ADO执行存储过程的问题,出现“服务器连接错误”然后runtime error,求大虾指教!
时间:2011-12-12
来源:互联网
_ConnectionPtr m_pconnection;
_RecordsetPtr m_precordset;
m_pconnection.CreateInstance("ADODB.Connection");
m_pconnection->Open("Driver={SQL Server};Server=.;Database=XSL;UID=sa;PWD=83432296","","",adModeUnknown);
try
{
m_pconnection->Open("Driver={SQL Server};Server=.;Database=XSL;UID=sa;PWD=wxy","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("服务器连接错误~!"); //处理错误吧
}
_CommandPtr m_pCommand; //还是智能指针
m_pCommand.CreateInstance("ADODB.Command"); //实例
m_pCommand->ActiveConnection = m_pconnection; //设置连接,别忘了啊
m_pCommand->CommandText = "Test"; //存储过程为Test
_ParameterPtr m_pid;
m_pid.CreateInstance("ADODB.Parameter");
_ParameterPtr m_pname;
m_pname.CreateInstance("ADODB.Parameter");
_ParameterPtr m_psdate;
m_psdate.CreateInstance("ADODB.Parameter");
_ParameterPtr m_pret;
m_pret.CreateInstance("ADODB.Parameter");
m_pid = m_pCommand->CreateParameter("id",adInteger,adParamInput,-1,(_variant_t)"10");
m_pCommand->Parameters->Append(m_pid);
m_pname=m_pCommand->CreateParameter("Name",adVarChar,adParamInput,20,(_variant_t)"songwenfeng");
m_pCommand->Parameters->Append(m_pname);
m_psdate = m_pCommand->CreateParameter("sdate",adVarChar,adParamInput,32,(_variant_t)"2011-11-1");
m_pCommand->Parameters->Append(m_psdate);
m_pret = m_pCommand->CreateParameter("ret",adChar,adParamOutput,1);
m_pCommand->Parameters->Append(m_pret);
m_precordset= m_pCommand->Execute(NULL,NULL,adCmdStoredProc);
while (!m_precordset->adoEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)m_precordset->GetCollect("name"));
m_precordset->MoveNext();
}
m_pconnection->Close();
CoUninitialize();
存储过程是 CREATE PROCEDURE Test
@id int,
@Name varchar(20),
@sdate datetime,
@ret char(1) output
AS
insert into VCStorproc values(@id,@Name,@sdate)
if @@error=0
set @ret=1
else
set @ret=0
go
_RecordsetPtr m_precordset;
m_pconnection.CreateInstance("ADODB.Connection");
m_pconnection->Open("Driver={SQL Server};Server=.;Database=XSL;UID=sa;PWD=83432296","","",adModeUnknown);
try
{
m_pconnection->Open("Driver={SQL Server};Server=.;Database=XSL;UID=sa;PWD=wxy","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("服务器连接错误~!"); //处理错误吧
}
_CommandPtr m_pCommand; //还是智能指针
m_pCommand.CreateInstance("ADODB.Command"); //实例
m_pCommand->ActiveConnection = m_pconnection; //设置连接,别忘了啊
m_pCommand->CommandText = "Test"; //存储过程为Test
_ParameterPtr m_pid;
m_pid.CreateInstance("ADODB.Parameter");
_ParameterPtr m_pname;
m_pname.CreateInstance("ADODB.Parameter");
_ParameterPtr m_psdate;
m_psdate.CreateInstance("ADODB.Parameter");
_ParameterPtr m_pret;
m_pret.CreateInstance("ADODB.Parameter");
m_pid = m_pCommand->CreateParameter("id",adInteger,adParamInput,-1,(_variant_t)"10");
m_pCommand->Parameters->Append(m_pid);
m_pname=m_pCommand->CreateParameter("Name",adVarChar,adParamInput,20,(_variant_t)"songwenfeng");
m_pCommand->Parameters->Append(m_pname);
m_psdate = m_pCommand->CreateParameter("sdate",adVarChar,adParamInput,32,(_variant_t)"2011-11-1");
m_pCommand->Parameters->Append(m_psdate);
m_pret = m_pCommand->CreateParameter("ret",adChar,adParamOutput,1);
m_pCommand->Parameters->Append(m_pret);
m_precordset= m_pCommand->Execute(NULL,NULL,adCmdStoredProc);
while (!m_precordset->adoEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)m_precordset->GetCollect("name"));
m_precordset->MoveNext();
}
m_pconnection->Close();
CoUninitialize();
存储过程是 CREATE PROCEDURE Test
@id int,
@Name varchar(20),
@sdate datetime,
@ret char(1) output
AS
insert into VCStorproc values(@id,@Name,@sdate)
if @@error=0
set @ret=1
else
set @ret=0
go
作者: fengyuzhe1570 发布时间: 2011-12-12
先看你的SQL语句是否正确,这种一般可能是SQL语句有问题
作者: oyljerry 发布时间: 2011-12-12
你的连接对象怎么Open了两次,而且密码不一样,用一次就可以了!
把你的连接字符串改改:
"Provider=SQLOLEDB;Server=(local);Database=XSL; uid=sa;Password=83432296"
把你的连接字符串改改:
"Provider=SQLOLEDB;Server=(local);Database=XSL; uid=sa;Password=83432296"
作者: sdl2005lyx 发布时间: 2011-12-12
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28