从中午一直忙到现在,竟然是为了一个小小的CString!!
代码如下:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSql;
strSql.Format("select * from user where 用户名 =\'%s\'",UserName);
BSTR bstrSQL = strSql.AllocSysString(); try
{
m_pConnection->Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb", "","",adModeUnknown);
m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,
adOpenDynamic,adLockOptimistic,adCmdText); //就是这一句有问题
SysFreeString(bstrSQL);
}catch{
............}当程序执行到有注释的那一行时,出现了一个对话框:Unhandled exception in jiankong.exe : 0xC0000005:Access Violation.然后程序跳到
_AFX_INLINE CStringData* CString::GetData() const
{ ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)-1; }
这里就不动。敢问各位高手这是怎么回事,小弟好歹也写了几年代码了,这却栽在一个小小的CString中,郁闷至极
代码如下:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSql;
strSql.Format("select * from user where 用户名 =\'%s\'",UserName);
BSTR bstrSQL = strSql.AllocSysString(); try
{
m_pConnection->Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb", "","",adModeUnknown);
m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,
adOpenDynamic,adLockOptimistic,adCmdText); //就是这一句有问题
SysFreeString(bstrSQL);
}catch{
............}当程序执行到有注释的那一行时,出现了一个对话框:Unhandled exception in jiankong.exe : 0xC0000005:Access Violation.然后程序跳到
_AFX_INLINE CStringData* CString::GetData() const
{ ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)-1; }
这里就不动。敢问各位高手这是怎么回事,小弟好歹也写了几年代码了,这却栽在一个小小的CString中,郁闷至极
看看你的查询语句中是不是类型和数据库中数据的类型不一样,或者是字段名有错误
已经试过了,还是不行,问题应该出在CString与BSTR之间
改为m_pConnection->Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=\\data\\data.mdb", "","",adModeUnknown);
也会出现同样的问题
改为: m_pRecordset->Open((_variant_t)strSql ....关注!
还是不行,老问题
我单步调试到BSTR bstrSQL = strSql.AllocSysString(); 这一句时,strSql的值为"select * from user where 用户名 ='系统管理员'",这句SQL语句是完全没有问题的啊
GetDlgItemText(IDC_Server,sServer);
GetDlgItemText(IDC_DBS,sDatabase);
GetDlgItemText(IDC_USER,sUser);
GetDlgItemText(IDC_PWD,sPwd);
sServer.TrimRight(' ');
sDatabase.TrimRight(' ');
sUser.TrimRight(' ');
sPwd.TrimRight(' '); sConnect="driver={SQL Server};Server="+sServer+";DATABASE="+sDatabase+";UID="+sUser+";PWD="+sPwd;
try
{
// hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
// hr=m_pRecordset.CreateInstance("ADODB.Recordset");
hr1 =m_pConnection.CreateInstance(__uuidof(Connection));
hr2 =m_pRecordset.CreateInstance(__uuidof(Recordset));
hr3=m_pCommand.CreateInstance(__uuidof(Command));
//if(SUCCEEDED(hr))
if(hr1>=0 && hr2>=0 && hr3>=0)
{
//AfxMessageBox("CreateInstance OK!");
m_pConnection->ConnectionTimeout = 25;///设置超时时间为25秒
hr1 = m_pConnection->Open((LPCTSTR)sConnect,"","",adModeUnknown);///注意用的是 sConnect
}
}
用类似这样看行吗
能说得详细点吗
我在学JSP时碰到过不能用user.换个名行吗?
CString str;
str.Format("\'aaa\'");
BSTR bstr = str.AllocSysString(); try
{
WCHAR sz[1024];
wcscpy(sz, bstr);
SysFreeString(bstr);
}
catch...
strTmp = "dsdsds";
strTmp.Format( "%sfdsfds", strTmp);