我的数据库连接代码如下:
CString strConnection;
strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;")
_T("Data Source=note.mdb"));
m_DBCn.Open((LPCTSTR)strConnection); //打开程序数据库
m_Rs.SetDatabase(&m_DBCn);
m_Rs.Open(_T("select * from test;"));
其中变量的定义是:
CADORecordset m_Rs;
CADODatabase m_DBCn;
程序运行后出错:
错误如下:
CADORecordset Error
Code = 80004003
Code meaning = 无效指针
Source = (null)
Description = (null)
请问错误在哪?
CString strConnection;
strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;")
_T("Data Source=note.mdb"));
m_DBCn.Open((LPCTSTR)strConnection); //打开程序数据库
m_Rs.SetDatabase(&m_DBCn);
m_Rs.Open(_T("select * from test;"));
其中变量的定义是:
CADORecordset m_Rs;
CADODatabase m_DBCn;
程序运行后出错:
错误如下:
CADORecordset Error
Code = 80004003
Code meaning = 无效指针
Source = (null)
Description = (null)
请问错误在哪?
解决方案 »
- DLL项目的头文件、源文件、宏,各位有什么设计经验,我是乱套了
- VC中combobox的问题
- 关于SetTimer 一个比较菜的问题,大家莫笑我!
- 如何在程序中向qq登陆界面填写qq号码和密码?
- 为什么在CListView中内容添加进去了,却显示为空白???
- 請教関于SUM()在SQL語句中的用法
- 静态拆分窗口后,如何处理WM_SIZE事件?以保证各部分的大小比例保持不变?
- 如何在VC中建立一个数据库
- 居然直接从MSDN上拿下来的示范代码也会不好使?(解释必送分)
- wyzegg(蛋)再给你30分,讲讲DCOM系统的错误处理问题吧!
- 我的UDP广播表现的不太正常
- 访问DCOM 得到远程机器的 主板等信息 ExecQuery时 拒绝访问 怎么解决?
http://www.vckbase.com/document/viewdoc/?id=1557
select * from test;是不是多了一个分号?
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection.CreateInstance(__uuidof(Connection));
m_pCommand.CreateInstance(__uuidof(Command));
try
{
//取得当前程序的完整路径,并拼出数据库的路径
char szPath[MAX_PATH] = "" ;
GetModuleFileName(NULL,szPath,MAX_PATH) ;
PathAppend( szPath , _T("..\\iccard.mdb") ) ;
CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" ;
strConnection += szPath ;
strConnection += ";Persist Security Info=True;Jet OLEDB:Database Password=hello" ;
m_pConnection->Open((_bstr_t)strConnection,"","",-1) ;
}
catch(_com_error e)
{
return ERROR_DATABASECONNECT_FAILURE;
}
catch ( ... )
{
return ERROR_DATABASE_UNKNOWNERROR ;
} return ERROR_DATABASE_SUCCESSED;
}
/*
该函数将ADO的错误进行格式化,并以CString 的形式返回错误信息。
*/
CString CAdoOperator::GetErrorInfo()
{
CString ErrorMessage,temp; if( NULL == m_pConnection )
{
ErrorMessage = "错误的数据连接! ";
return ErrorMessage ;
}
long errorcount=m_pConnection->GetErrors ()->GetCount ();
_bstr_t add; for (short i=0;i<errorcount;i++)
{
add=m_pConnection->GetErrors ()->GetItem (_variant_t((short)i))->GetDescription ();
temp = (char *)add;
ErrorMessage +=temp;
}
return ErrorMessage;
}
_ConnectionPtr m_pConnection;
AfxOleInit();
CDialog::OnInitDialog();
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
MessageBox("数据库连接失败,确认数据库*.mdb是否在当前路径下!");
return FALSE;
}
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->CursorLocation = adUseClient;//需要排序的话必须要加上的
try
{
m_pRecordset->Open("SELECT * FROM table",// 查询table中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
} catch(_com_error e)
{
MessageBox("失败");
return FALSE;
}
我的程序代码 给你参考一下
#import "C:\program files\common files\System\ado\msado15.dll" no_namespace \
rename("EOF","EndOfFile") \
rename("LockTypeEnum","newLockTypeEnum")\
rename("DataTypeEnum","newDataTypeEnum")\
rename("FieldAttributeEnum","newFieldAttributeEnum")\
rename("EditModeEnum","newEditModeEnum")\
rename("RecordStatusEnum","newRecordStatusEnum")\
rename("ParameterDirectionEnum","newParameterDirectionEnum")
如果你的系统不是安装在C盘的话就把#import 后面的C改成系统所有的盘
(2).在C***App类的
public:下加入
_RecordsetPtr m_pADOSet;
bool ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL);
在private:下加入_ConnectionPtr ADOConn;
在class C***App : public CWinApp
{
...
};之后#endif之前加入extern C***App theApp; (3)在BOOL C***App::InitInstance()函数中Enable3dControls(); // Call this when linking to MFC statically这一行下面加入
if( FAILED(::CoInitialize(NULL)) )
{
AfxMessageBox("ADO Init failed");
return false;
}
try
{
ADOConn.CreateInstance(__uuidof(Connection));
ADOConn->Open("DSN=OBDC数据源;Provider=MSDASQL","用户","密码", adConnectUnspecified);//这一行要自已修改
}
catch(_com_error &e)
{
CString err;
err.Format("%s", (char*)(e.Description()) );
AfxMessageBox(err);
}
catch(...)
{
AfxMessageBox("Unknown Error...");
}
m_pADOSet.CreateInstance(__uuidof(Recordset));
并在文件最后加上如下代码:
bool C***App::ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL)
{
if ( ADOSet->State == adStateOpen) ADOSet->Close();
try
{
ADOSet->Open(strSQL, ADOConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown);
return true;
}
catch(_com_error &e)
{
CString err;
err.Format("ADO Error: %s",(char*)e.Description());
AfxMessageBox(err);
return false;
}
}
最后就可以在登录时执行SQL语句了,比如用户为CString strUser, 密码是CString strPwd;数据库表是user_table(user_id, user_name, user_pwd)则
_variant_t strQuery, Holder;
strQuery = "select * from user_table where user_name='"+strUser +"' and user_pwd='"+ strPwd +"'";
theApp.ADOExecute(theApp.m_pADOSet, strQuery);
int iCount = theApp.m_pADOSet->GetRecordCount();
if ( 0==iCount )
{
AfxMessageBox(_T("密码错误"), MB_ICONEXCLAMATION);
return;
}
else
{
AfxMessageBox(_T("登录成功"), MB_ICONEXCLAMATION);
}
在建立 connection 对象之前 调用CoInitialize(NULL);
否则不能建立connection对象