急!!vc中用ADO连接sql数据库运行时出现runtime error

kpg20035528 2007-08-10 11:22:42
各位大哥大姐,帮帮忙吧。我在用vc环境下用ado连接sql数据库,编译链接都通过了
可是运行时出现以下错误,调试运行已经快一周了,真急死我了,恳请大家帮帮忙啊!
代码如下:
// stdafx.h : include file for standard system include files
#import "C:\WINDOWS\system\msado15.dll" no_namespace rename("EOF","adoEOF")
//已经在stdafx.h中引入ado
BOOL CDATAApp::InitInstance()
{
AfxEnableControlContainer();

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CDATADlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
//初始化com库
CoInitialize(NULL);
//实例化三个com对象
mpcon.CreateInstance(_uuidof(Connection));
mpcom.CreateInstance(_uuidof(Command));
mprcd.CreateInstance(_uuidof(Recordset));

return FALSE;
}

void CDATADlg::OnButInsert()
{
//此函数作用:建立与数据库连接,数据库中插入数据
// TODO: Add your control notification handler code here
//建立与数据库连接,在数据库中建立新表
try{
/* _variant_t vNULL;
vNULL.vt = VT_ERROR;
vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数
mpcom->ActiveConnection = mpcon;
mpcom->CommandText="CREATE TABLE STUDENT(NAME VARCHAR(16),AGE INT DEFAULT 20 )";
mpcom->Execute( &vNULL,&vNULL,adCmdText);///执行命令,取得记录集
*/
try{
str1="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=pubs;Data Source=LOCALWINDOWSNT\\ONE";
mpcon->ConnectionString=(_bstr_t)str1;//问题在这句话!!!运行到此,出现异常!!
HRESULT hresult;

hresult=mpcon->Open("","","",adConnectUnspecified);
if(SUCCEEDED(hresult))
{
//str="CREATE TABLE STUDENT(NAME VARCHAR(16),AGE INT DEFAULT 20 )";
str="select * from authors";
//_variant_t RecordsAffected;
//mprcd=mpcon->Execute((_bstr_t)str,NULL,adCmdText);
mprcd->Open((_bstr_t)str,(IDispatch *)mpcon,adOpenForwardOnly,adLockReadOnly,adCmdText);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息

}


//TRACE("%l",mpcom);
//
//GetName();
while(!(mprcd->adoEOF))
{
((CListBox *)GetDlgItem(IDC_LIST1))->AddString( (_bstr_t)mprcd->GetCollect("phone") );
mprcd->MoveNext();
//mprcd->GetCollect();函数返回值是_variant_t
}


// mprcd->close();
// mpcom->close();
// mpcon->close();
mprcd.Release();
mpcom.Release();
mpcon.Release();

CoUninitialize();
编译连接结果:
Deleting intermediate files and output files for project 'DATA - Win32 Debug'.
--------------------Configuration: DATA - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
c:\program files\microsoft visual studio\myprojects\data\debug\msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
Compiling...
DATA.cpp
DATADlg.cpp
Generating Code...
Linking...

DATA.exe - 0 error(s), 1 warning(s)
运行结果:

Runtime error!

Programe:...s\Microsoft Visual Studio\MyProjects\DATA\Debug\DATA.exe
This application has requested the Runtime to terminate it in an unsual way.
Please contact the application's support team for more information.
在一步一步的跟随下,发现执行这句话就弹出异常!
mpcon->ConnectionString=(_bstr_t)str1;//问题在这句话!!!运行到此,出现异常!!
我的连接字串应该没问题的,在桌面上建立.udl文件,然后用记事本方式打开,测试连接时,都已经连接成功了!
哎,可是就不知道问题到底是不是这句话?这个程序应该怎样改啊??
恳请大家帮帮忙,分析分析,给点建议啊!!
...全文
1224 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
boxer2011111 2011-12-06
  • 打赏
  • 举报
回复
试着用一些技巧获取连接字符串,然后复制粘贴过来吧,注意字符串的单\要换成双\\才能用希望帮到你。
具体的技巧有两种,一种是用VB;另一种是用桌面上建个文档的方式;你度娘一下吧~
shuyucao2 2011-10-19
  • 打赏
  • 举报
回复
我前一段时间也是碰到这个问题。后来发现这些都是很小的问题。出现这问题主要有:1.数据库字段跟插入的字段不匹配。还有就是操作数据库时%d %s这几个运用的不好。字符串一般的%s一半是要家单引号的。
sq15955853796 2011-10-18
  • 打赏
  • 举报
回复
最近也遇到了这样的问题,但是就是不知道这是怎么搞的
有了解的大神还请多多回复
小牛毛 2011-09-27
  • 打赏
  • 举报
回复
我也是 啊!哎哟I
海阔任鱼跃 2011-09-20
  • 打赏
  • 举报
回复
楼主如果解决了,,也教教我吧,,我也遇到类似的问题。
tianloveyou 2008-04-10
  • 打赏
  • 举报
回复
str1= "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=pubs;Data Source=LOCALWINDOWSNT\\ONE ";
mpcon-> ConnectionString=(_bstr_t)str1;//问题在这句话!!!运行到此,出现异常!!
把这些连接数据库的语句放到InitInstance() 应用程序初使化函数里.并且在对话框弹出之前.
内存泄漏 2008-04-10
  • 打赏
  • 举报
回复
mpcon-> ConnectionString=(_bstr_t)str1;//问题在这句话!!!运行到此,出现异常!!

把上面这句删了,然后把下面的语句:

hresult=mpcon-> Open( " ", " ", " ",adConnectUnspecified);

修改成:
hresult=mpcon-> Open( str1, " ", " ",adConnectUnspecified);

试试看..
AndyLZ 2008-04-09
  • 打赏
  • 举报
回复
LZ解决了吗,我也遇到了类似问题。。。
wang_wuhui 2007-09-05
  • 打赏
  • 举报
回复
调试,进函数看看..
shanhqk 2007-09-05
  • 打赏
  • 举报
回复
1.CDATAApp::InitInstance()和CDATADlg::OnButInsert()来看,这是一个对话框,而mpcon这种使用方法,我不知道LZ在哪里定义的,是两个文件都定义吗?还是在CDATAAPP里面定义的,如果是,那么为什么不使用theApp.mpcon,而直接使用mpcon,这是怀疑的第一个地方。
2.LZ可以跟踪一下,看mpcon的值是否为空或是一个随即的值?
2021‘someday 2007-09-04
  • 打赏
  • 举报
回复
http://blog.csdn.net/wang921718/archive/2007/07/19/1698490.aspx
看看我的BLOG,希望能对你有帮助!
sdklln 2007-08-14
  • 打赏
  • 举报
回复
好像是你的连接字符串有问题,
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=LOCALWINDOWSNT\\ONE
kpg20035528 2007-08-10
  • 打赏
  • 举报
回复
在调试过程中,出现两个Find source对话框
第一个内容:Please enter the path for CHKSTK.ASM
第二个内容:Please enter the path for TH_INST.CPP
还有另外两个对话框:
第一个:the thread oxED8has exited with code 128
第二个Microsoft Exception
Unhandle exception in DATA.EXE(KERNEL32.dll):oxE06D7363;Microsoft Exception

请问改怎么改啊?
kpg20035528 2007-08-10
  • 打赏
  • 举报
回复
不行啊,还是不行啊!
CoInitialize(NULL);
mpcon.CreateInstance(_uuidof(Connection));
mpcom.CreateInstance("ADODB.Command");
mprcd.CreateInstance(_uuidof(Recordset));

CDATADlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
结果还是弹出
Runtime error!

Programe:...s\Microsoft Visual Studio\MyProjects\DATA\Debug\DATA.exe
This application has requested the Runtime to terminate it in an unsual way.
Please contact the application's support team for more information.
guxingfeiyu 2007-08-10
  • 打赏
  • 举报
回复
对话框关闭了你再初始化数据库连接啊?把
//初始化com库
CoInitialize(NULL);
//实例化三个com对象
mpcon.CreateInstance(_uuidof(Connection));
mpcom.CreateInstance(_uuidof(Command));
mprcd.CreateInstance(_uuidof(Recordset));
放到CDATAdlg dlg;前面去

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧