急!!vc中用ADO连接sql数据库运行时出现runtime error
各位大哥大姐,帮帮忙吧。我在用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文件,然后用记事本方式打开,测试连接时,都已经连接成功了!
哎,可是就不知道问题到底是不是这句话?这个程序应该怎样改啊??
恳请大家帮帮忙,分析分析,给点建议啊!!