Connect the VC to the SQLSERVER Database

Source: Internet
Author: User
Welcome to the CC ++ programming Community Forum and interact with 2 million technical staff to enter voidsmsLogon: InitSQLServer (CStringserver, CStringdb, CStringUserName, CStringPwd) {m_pConnection.CreateInstance (ADODB. connection); CStringstrCn; strCn. empty (); strCnprov

Welcome to the C/C ++ programming Community Forum and interact with 2 million technicians> enter void smsLogon: InitSQLServer (CString server, CString db, CString UserName, CString Pwd) {m_pConnection.CreateInstance ("ADODB. connection "); CString strCn; strCn. empty (); strCn = "prov

Welcome to the C/C ++ programming Community Forum and interact with 2 million technical staff> enter

Void smsLogon: InitSQLServer (CString server, CString db, CString UserName, CString Pwd)

{

M_pConnection.CreateInstance ("ADODB. Connection ");

CString strCn;

StrCn. Empty ();

StrCn = "provider = SQLOLEDB; data source =" + server

+ "; Initial catalog =" + db

+ "; UserID =" + UserName

+ "; PassWord =" + Pwd;

_ Variant_t bcnstr = _ variant_t (strCn );

_ Variant_t bunstr = _ variant_t (UserName );

_ Variant_t bpwdstr = _ variant_t (Pwd );

// Open a connection

Try

{

M_pConnection-> Open (_ bstr_t (bcnstr), _ bstr_t (bunstr ),

_ Bstr_t (bpwdstr),-1); // adOpenUnspecified

}

Catch (_ com_error e)

{

CString errormessage;

Errormessage. format ("Warning: An exception occurred when connecting to the database. error message: % s; file: % s; row: % d \ n ", e. errorMessage (), _ FILE __, _ LINE __);

AfxMessageBox (errormessage );

}

Catch (...)

{

AfxMessageBox ("Warning: Unknown error occurred when connecting to the Database ");

}

}

Void smsLogon: ExitDB () // close the database connection when exiting

{

If (m_pConnection! = NULL)

{

M_pConnection-> Close (); // Close the connection

M_pConnection.Release (); // release an object

}

}

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.