连接sql2005数据库失败解决思路

连接sql2005数据库失败
本帖最后由 oyljerry 于 2013-11-30 00:11:19 编辑
BOOL CLZJDataBase::doQuery(CString strSQL, _RecordsetPtr &pRecordset)

 if (m_bOpened == FALSE) return FALSE;
 pRecordset.CreateInstance(__uuidof(Recordset));
 pRecordset->CursorLocation = adUseClient;
 HRESULT hr;
 try
 {
  hr = pRecordset->Open(_bstr_t(strSQL), m_Connection.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText); 
 }
 catch (_com_error e) 
 {
  CString strValue;
  CString strDNS = GetConnectString();
  m_Connection->Close();
  m_Connection.Release();
  
  strValue.Format(_T("<%s> %s--------\n"), GetTime(), strDNS);     
  GWriteLog(strValue);
  
  HRESULT hr = m_Connection.CreateInstance(__uuidof(Connection));
  hr = m_Connection->Open(_bstr_t(strDNS), "", "", -1);  
  
  strValue.Format(_T("<%s> ReConnect---------- SQL = %s\n"), GetTime(), strSQL);     
  GWriteLog(strValue);
  
  if(FAILED(hr))
  {
   CString strValue;
   strValue.Format(_T("<%s> Reconnect Error---------- SQL = %s\n"), GetTime(), strSQL);     
   GWriteLog(strValue);       
   _tprintf(strValue);     
   return FALSE;
  }
  else
  {
   m_bOpened = TRUE;
   hr = pRecordset->Open(_bstr_t(strSQL), m_Connection.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
   return TRUE;
  }
 } 
 return TRUE;
}
CString CLZJDataBase::GetConnectString()
{
CString strIP;
GetPrivateProfileString(_T("DataBase"), _T("IP"), _T(""), strIP.GetBufferSetLength(MAX_PATH), MAX_PATH, getPath() + "\\config.ini");
strIP.ReleaseBuffer();
strIP.TrimLeft();
strIP.TrimRight();

CString strPort;
GetPrivateProfileString(_T("DataBase"), _T("PORT"), _T(""), strPort.GetBufferSetLength(MAX_PATH), MAX_PATH, getPath() + "\\config.ini");
strPort.ReleaseBuffer();
strPort.TrimLeft();
strPort.TrimRight();

CString strDB;
GetPrivateProfileString(_T("DataBase"), _T("DB"), _T(""), strDB.GetBufferSetLength(MAX_PATH), MAX_PATH, getPath() + "\\config.ini");
strDB.ReleaseBuffer();
strDB.TrimLeft();
strDB.TrimRight();

CString strLoginName;
GetPrivateProfileString(_T("DataBase"), _T("LoginName"), _T(""), strLoginName.GetBufferSetLength(MAX_PATH), MAX_PATH, getPath() + "\\config.ini");
strLoginName.ReleaseBuffer();
strLoginName.TrimLeft();
strLoginName.TrimRight();

CString strPassword;
GetPrivateProfileString(_T("DataBase"), _T("Password"), _T(""), strPassword.GetBufferSetLength(MAX_PATH), MAX_PATH, getPath() + "\\config.ini");
strPassword.ReleaseBuffer();
strPassword.TrimLeft();
strPassword.TrimRight();

CString strDNS = "Provider=SQLOLEDB;SERVER=" + strIP  + _T(",") + strPort + "; DATABASE=" + strDB + ";UID="+strLoginName+";PWD=" + strPassword; //不能有空格
return strDNS;
}

连接sql2005数据库失败解决思路

config.ini
[DataBase]
LoginName=sa
Password=123
IP=71A5E5EC09B449B\\XIAOYE
DB=yschat
Port=2820
[software]
ver=100
tcpport=8500
ServerType=0
[FtpServer]
IP=127.0.0.1
port=21
name=xiaoye
password=123

提示连接失败,怎么办?谢谢!
------解决方案--------------------
不用程序,直连连得上不?
------解决方案--------------------
错误信息是什么
------解决方案--------------------
关闭防火墙,包括WIN自带的防火墙
------解决方案--------------------
是不是没有找到实例名