我用ADO访问SQL SERVER2000数据库,程序在本机上无任何错误,但在局域网其它机器上运行,却出错。以下为源代码的一部分。主程式中有:objData.ConnectTo();
str.Format("select * from MANAGER where NICKNAME = '%s' and PASSWORD = '%s'",mstrname,mstrpwd);//mstrname,mstrpwd为输入的用户名和密码
objData.OpenTable(str);
class CADOSQL
{
// 属性
public:
_ConnectionPtr mcConnection;
_RecordsetPtr mcRecordset;
CString mstrSource;
bool mbConnected;
CString mLastError;
。CADOSQL objData;........
以下为函数代码
bool CADOSQL::ConnectTo()
{
HRESULT hr;
_bstr_t source("Driver={SQL Server};Server=CHOCOBO;Database=PIZZA4FUN");
_bstr_t user("sa");
_bstr_t pwd("");
try
{
hr = mcConnection.CreateInstance(_uuidof(Connection));
hr = mcConnection->Open(source, user, pwd, adConnectUnspecified);
hr = mcRecordset.CreateInstance(_uuidof(Recordset));
mbConnected = true;
}
catch(_com_error &e)
{
CString strDescription, str1, str2;
if( mLastError != e.ErrorMessage() )
{
mLastError = e.ErrorMessage();
str1 = (LPCTSTR)e.Description();
int w = str1.ReverseFind(']');
//w要加1
str2 = str1.Mid(0,w+1);
str1.Delete(0,w+1);
strDescription.Format("%s\n数据库访问异常\n\n%s\n%s", str2, e.ErrorMessage(), str1);
//用LPCTSTR避免错误,CSTRING内部不详
static CMyMessageBox box((LPCTSTR)strDescription);
}
TRACE("_com_error exception thrown.\n");
str1.Format("HRESULT = %081x \n", e.Error());
TRACE(str1);
DisplayError(mcConnection);
mbConnected = false;
}
if( mbConnected ) mstrSource = (const char*)source;
return mbConnected;
}
//打开表并返回指定的记录集,需用户自己调用->Close方法将其关闭bool CADOSQL::OpenTable(CString strCommand)
{
if( !mbConnected ) return false;HRESULT hr;
_bstr_t query = (_bstr_t)strCommand;
_bstr_t source = (_bstr_t)mstrSource;
try
{
hr = mcRecordset->Open(query, source, adOpenDynamic, adLockOptimistic, adCmdText);
return true;
}
catch(_com_error &e)
{
*****************************************************程序执行中断在此
CString strDescription, str1, str2;
if( mLastError != e.ErrorMessage() )
{
mLastError = e.ErrorMessage();
str1 = (LPCTSTR)e.Description();
int w = str1.ReverseFind(']');
//w要加1
str2 = str1.Mid(0,w+1);
str1.Delete(0,w+1);
strDescription.Format("%s\n数据库访问异常\n\n%s\n%s", str2, e.ErrorMessage(), str1);
//用LPCTSTR避免错误,CSTRING内部不详
static CMyMessageBox box((LPCTSTR)strDescription);
}
TRACE("_com_error exception thrown.\n");
str1.Format("HRESULT = %081x \n", e.Error());
TRACE(str1);
DisplayError(mcConnection);
return false;
}
}如果不设断点,程式报错:数据库访问异常,IDispatch error #3149
用户"(null)"登录失败。原因:未与信任SQL Server连接相关联。