ADO连接数据库有关问题,总出现“无效指针”异常
ADO连接数据库问题,总出现“无效指针”错误
BOOL CSqlDlg::Init()
{
CString m_strPath;
CString m_strJDCFile;
CString m_strJDC;
CoInitialize(NULL); //对连接进行初始化
// m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection.CreateInstance("ADODB.Connection");
try
{
//AfxMessageBox(cJDC);
m_pConnection->Open((LPCTSTR)"Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=JDC;Data Source=BR51XI7JYQMJPTS",(LPCTSTR)(""),(LPCTSTR)(""),adConnectUnspecified);
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( e.ErrorMessage( ) );
//显示错误信息 "无效指针"
AfxMessageBox(strError);
return FALSE;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
return TRUE;
}
------解决方案--------------------
把用户名和密码去掉,填在2、3参数里~另外再不好使,你用工具测试下你够简单额字符串是否正确
------解决方案--------------------
用这个代码看报什么错误。
------解决方案--------------------
直接用数据库自带的客户端用密码等连接试试
BOOL CSqlDlg::Init()
{
CString m_strPath;
CString m_strJDCFile;
CString m_strJDC;
CoInitialize(NULL); //对连接进行初始化
// m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection.CreateInstance("ADODB.Connection");
try
{
//AfxMessageBox(cJDC);
m_pConnection->Open((LPCTSTR)"Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=JDC;Data Source=BR51XI7JYQMJPTS",(LPCTSTR)(""),(LPCTSTR)(""),adConnectUnspecified);
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( e.ErrorMessage( ) );
//显示错误信息 "无效指针"
AfxMessageBox(strError);
return FALSE;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
return TRUE;
}
------解决方案--------------------
把用户名和密码去掉,填在2、3参数里~另外再不好使,你用工具测试下你够简单额字符串是否正确
------解决方案--------------------
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
if (FAILED(hr))
{
_com_error e(hr);
AfxMessageBox(e.ErrorMessage());
return;
}
用这个代码看报什么错误。
------解决方案--------------------
直接用数据库自带的客户端用密码等连接试试