Sqlserver connection and settings

Source: Internet
Author: User

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 ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.