MFC连接数据库

void CASDDlg::OnButton8() //****************************************************************************连接
{
  try                 
{	//if (sta==1)//初始化OLE/COM库环境        
   //{
       // return;       
   // }
//	if (!AfxOleInit())//初始化OLE/COM库环境        
  // {
  //      AfxMessageBox("OLE初始化出错!");       
   // }

	  using namespace std;
	  ifstream fin("D:\\htcxaddress\\address.txt");
	  char sentence[101];
	  fin.getline(sentence, 100);
	  fin.close();
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection)); // 打开本地数据库
	      m_pRecordset.CreateInstance(__uuidof(Recordset));
hr=m_pConnection->Open("driver={SQL Server};Server=10.35.9.85,1433;DATABASE=HTCX;UID=sa;PWD=Admin123;","","",adModeUnknown); 

//hr = m_pConnection->Open(sentence, "", "", adModeUnknown);

//Provider=SQLOLEDB.1;Password=Admin123;Persist Security Info=True;User ID=sa;Initial Catalog=TEST2;Data Source=WINDOWS-TCPNWT3\SQLEXPRESS
//hr=m_pConnection->Open("Provider=SQLOLEDB.1;Password=Admin123;Persist Security Info=True;User ID=sa;Initial Catalog=TEST2;Data Source=WINDOWS-TCPNWT3\\SQLEXPRESS","","",adModeUnknown); 
 if (!SUCCEEDED(hr)) {
            AfxMessageBox("连接数据库失败");
        }
  }
catch(_com_error e)
{
	CString errormessage;
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
   AfxMessageBox(errormessage);
} 
       OnButton1() ; 
	  // sta=1;   	
}

你可能感兴趣的:(MFC)