代码
(1)CAdoDB::CAdoDB()
{
::CoInitialize(NULL); //调用ColInitialize初始化环境
}CAdoDB::~CAdoDB()
{
::CoUninitialize(); //释放COM对象
} BOOL CAdoDB::Open(LPCTSTR lpszConnect,long lOptions)
{
m_pConn.CreateInstance("ADODB.Connection");//创建Connection对象
//初始化Recordset指针
m_pRs.CreateInstance(_uuidof(Recordset));
m_strConn=lpszConnect;
if(m_strConn.IsEmpty())
{
return FALSE;
}
if(IsOpen()) Close();
try
{
//连接数据库
return (m_pConn->Open(_bstr_t(m_strConn),"","",lOptions)==S_OK);
}
catch(_com_error e)//异常处理
{
AfxMessageBox(e.ErrorMessage());
}
return FALSE;
}(2)void CConfigView::OnBtnDbcon() 
{
if(!UpdateData()) return;//////////////////////////
CString sql;
sql.Format("Provider=SQLOLEDB.1;Password=dbFin;"
"Persist Security Info=False;User ID=dbFin;"
"Initial Catalog=FinanceDB;Data Source=LLITTLEMARK\SQLDB",
m_strPwd,m_strUser,m_strDBName,m_strServer);
//建立数据库的连接和加载数据
if(g_adoDB.Open(sql)==TRUE)
InitCtrlData();}                  编译运行成功,但在界面上要求输入SQL SERVER服务名,数据库名,用户名和用户密码,点连接按钮进行连接却怎么也连不上,改了N次了也不行,都是报  ”未指定的错误或“是IDispatch error #3149,如能帮助,感激不尽

解决方案 »

  1.   


    sql.Format("Provider=SQLOLEDB.1;Password=dbFin;" 
    "Persist Security Info=False;User ID=dbFin;" 
    "Initial Catalog=FinanceDB;Data Source=LLITTLEMARK\SQLDB", 
    m_strPwd,m_strUser,m_strDBName,m_strServer);
    //这句写错了吧,都没格式化进去。
      

  2.   

     就是不格式化试了也一样错啊,捕捉错误,显示信息如下:
                                                   
    错误编号:80040e4d
    错误信息:IDispatch error #3149
    错误源:Microsoft OLE DB Provider for SQL Server
    错误描述:用户dbFin登录失败如能解决错误,小弟感激不尽