Sqlserver database connection configuration and dataset settings
Hresult hR = s_false;
_ Recordsetptr m_qryrcdset;
_ Connectionptr m_pconnection;
HR = m_pconnection.createinstance ("ADODB. Connection ");
If (failed (HR ))
Return false;
Try
{
M_pconnection-> connectiontimeout = 8;
Cstring strconnect = "provider = sqloledb.1; Data Source =" + (Database Server IP address) + "; initial catalog =" + (Database Name) + "; user id ="
+ (Username) + "; Pwd =" + (password );
M_pconnection-> open (_ bstr_t (strconnect), "", "", admodeunknown );
}
Catch (_ com_error e) // catch an exception
{
Afxmessagebox ("database connection failed! "+ Cstring (E. errormessage ()));
Assert (false );
Return false;
}
HR = m_qryrcdset.createinstance ("ADODB. recordset ");
If (failed (HR ))
Return false;
Try
{
M_qryrcdset-> cursortype = adopenstatic;
M_qryrcdset-> cursorlocation = aduseclient;
HR = m_qryrcdset-> open (_ bstr_t (sqlstr), _ variant_t (idispatch *) m_pconnection, true), adopendynamic, adlockpessimistic, ad1_text );
Ado_longptr COUNT = m_qryrcdset-> getrecordcount ();
If (count> 0)
M_qryrcdset-> movefirst ();
}
Catch (_ com_error e) // catch an exception
{
E. Description ();
Afxmessagebox (E. Description ());
Afxmessagebox ("database connection failed! "+ Cstring (E. errormessage ()));
Assert (false );
Return false;
}
Add the following statement to the stdafx. h file:
# Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")