VC ++ uses ADO to connect to the SQL Server database

Source: Internet
Author: User

Basic steps:

1. Create a dialog box-based application adodatabase.

2. Create an ADO class library for connection. In the system menu, insert-> new class, and select generic class for class type. After name, enter the class name "adoconn" and click OK. In this way, the adoconn. h and adoconn. cpp files are automatically generated on the project.

3. in adoconn. import An ADO dynamic link library msado15.dll in H and add the code: # import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" no_namespace Rename ("EOF ", "adoeof") Rename ("Bof", "adobof"), at the same time in adoconn. h defines public: // Add a pointer to the connection object: _ connectionptr m_pconnection; // Add a pointer to the recordset object: _ recordsetptr m_precordset.

4. m_pconnection-> open (_ bstr_t connectionstring, _ bstr_t userid, _ bstr_t password, Long Options) operation. Connectionstring is the ADO settings in SQL Server in VC ++. For example, provider = sqloledb; Integrated Security = sspi; persist Security info = false; initial catalog = suntest; Data Source = WWW-ED1F301E78B; user id = sa; Password =;

Data source is the server name, which can be replaced by the server. or (local); initial catalog can replace the name of the database you want to use with the database; Integrated Security = sspi indicates that the integration mode is used. At this time, the UID and PWD settings do not work; the UID and PWD settings take effect only when integrated security = false indicates mixed mode.

The options parameter indicates the commandtext type: adw.text-text command; adcmdtable-table name adw.proc-stored procedure; adcmdunknown-Unknown

5. Add two member functions: (1) execute the query function _ recordsetptr & getrecordset (_ bstr_t bstrsql); (2) execute the SQL statement bool executesql (_ bstr_t bstrsql ).

The complete code of the two files is provided:

/* ---------------- Adoconn. h file -------------------------------------------------*/

// Adoconn. h: interface for the adoconn class.

# Import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" no_namespace Rename ("EOF", "adoeof") Rename ("Bof", "adobof ")

# If! Defined (afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _)

# Define afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _

# If _ msc_ver> 1000

# Pragma once

# Endif // _ msc_ver> 1000

Class adoconn

{

// Define variables

Public:

// Add a pointer to the connection object:

_ Connectionptr m_pconnection;

// Add a pointer to the recordset object:

_ Recordsetptr m_precordset;

// Define the Method

Public:

Adoconn ();

Virtual ~ Adoconn ();

// Initialize-connect to the database

Void oninitadoconn ();

// Execute the query

_ Recordsetptr & getrecordset (_ bstr_t bstrsql );

// Execute the SQL statement, insert update _ variant_t

Bool executesql (_ bstr_t bstrsql );

Void exitconnect ();

};

# Endif //! Defined (afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _)

/*--------------------------------------------------------------------------------*/

/* --------------------- Adoconn. cpp file ---------------------------*/

# Include "stdafx. H"

# Include "adoconn. H"

# Ifdef _ debug

# UNDEF this_file

Static char this_file [] =__ file __;

# Define new debug_new

# Endif

Adoconn: adoconn ()

{

}

Adoconn ::~ Adoconn ()

{

}

// Initialize-connect to the database

Void adoconn: oninitadoconn ()

{

// Initialize the OLE/COM library Environment

: Coinitialize (null );

Try

{

// Create a connection object

M_pconnection.createinstance ("ADODB. Connection ");

// Set the connection string, which must be of the BSTR or _ bstr_t type

_ Bstr_t strconnect = "provider = sqloledb; server = cz; database = hrsys; uid = sa; Pwd = sa ;";

M_pconnection-> open (strconnect, "", "", admodeunknown );

}

// Catch exceptions

Catch (_ com_error E)

{

// Display the error message

Afxmessagebox (E. Description ());

}

}

// Execute the query

_ Recordsetptr & adoconn: getrecordset (_ bstr_t bstrsql)

{

Try

{

// Connect to the database. If the connection object is null, reconnect to the database.

If (m_pconnection = NULL)

Oninitadoconn ();

// Create a record set object

M_precordset.createinstance (_ uuidof (recordset ));

// Obtain records in the table

M_precordset-> open (bstrsql, m_pconnection.getinterfaceptr (), adopendynamic, adlockoptimistic, adshorttext );

}

// Catch exceptions

Catch (_ com_error E)

{

// Display the error message

Afxmessagebox (E. Description ());

}

// Return record set

Return m_precordset;

}

// Execute the SQL statement, insert update _ variant_t

Bool adoconn: executesql (_ bstr_t bstrsql)

{

// _ Variant_t recordsaffected;

Try

{

// Whether the database has been connected

If (m_pconnection = NULL)

Oninitadoconn ();

// Execute method of the connection object :( _ bstr_t commandtext,

// Variant * recordsaffected, long options)

// Commandtext is a command string, usually an SQL command.

// The recordsaffected parameter indicates the number of rows affected by the operation,

// The options parameter indicates the commandtext type: adcmdtext-text command; adcmdtable-Table Name

// Adw.proc-stored procedure; adcmdunknown-Unknown

M_pconnection-> execute (bstrsql, null, ad1_text );

Return true;

}

Catch (_ com_error E)

{

Afxmessagebox (E. Description ());

Return false;

}

}

Void adoconn: exitconnect ()

{

// Close the record set and Connection

If (m_precordset! = NULL)

M_precordset-> close ();

M_pconnection-> close ();

// Release the environment

: Couninitialize ();

}

Related Article

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.