C + + read SQL Server sample share _c language

Source: Internet
Author: User

Copy Code code as follows:

ReadSqlConsole.cpp: Master project file.

#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <iomanip>//for setw ()
#include "Windows.h"

#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename ("EOF", "Endoffile")

using namespace System;
using namespace Std;

int main (array<system::string ^> ^args)
{
:: CoInitialize (NULL);

Type is defined in Msado15.dll
_RecordsetPtr M_precordset ("ADODB"). Recordset ");
_connectionptr m_pconnection ("ADODB"). Connection ");

_bstr_t bstrsql ("select * from Er_order where pactnumber like ' 13010000% ' ORDER by Pactnumber"); The corresponding SQL statement

Try
{
Clog << "Connecting to Database ..." << Endl;
M_pconnection.createinstance ("ADODB.") Connection ");//Create Connection Object
Set connection string, must be BSTR or _bstr_t type
_bstr_t strconnect = "provider=sqloledb; Server= "database instance";D atabase= "database name"; uid= "Database User"; pwd= "password"; ";
M_pconnection->open (StrConnect, "", "", admodeunknown);/server Connection

if (m_pconnection = NULL)
{
Std::cerr << "Lind data error!\n";
}

M_precordset.createinstance (__uuidof (Recordset));//Create Recordset object

Get records in a table
M_precordset->open (Bstrsql, M_pconnection.getinterfaceptr (), adopendynamic, adLockOptimistic, AdCmdText);

_variant_t Pactnumber, origcount;//the field in the corresponding library

        cout << "--------------contract list--------------" << Endl;
        while (!m_precordset->endoffile)
         {
            pactnumber = m_ Precordset->getcollect ("Pactnumber");
            origcount = M_precordset->getcollect (" Origcount ");
            cout << Contract No.: "<< SETW (+) < < (char*) (_bstr_t) Pactnumber;
            cout << "   signing Amount:" << SETW (7) << (char*) (_bstr_t) Origcount << Endl;

M_precordset->movenext ()//Next record
}
M_precordset->close ()//Close Recordset
}

catch (_com_error E)//catch exception
{
Cerr << "\nerror:" << (char*) e.description ();
}

if (m_pconnection->state)
{
M_pconnection->close ();
}

:: CoUninitialize ();

Console::readline ();

return 0;
}

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.