本机访问数据库和异机访问数据库有什么区别?
我用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连接相关联。
推荐阅读


讨论区