Use C ++ to access SQL Server 2000 instances
I. Introduction to ADO
ADO (ActiveX Data Object) is a new interface for Microsoft database application development. It is a high-level database access technology built on top of ole db. It is not only easy to use, but also flexible. it is a good choice for C ++ to use databases for rapid development.
I don't need to talk about the theory here. There are a lot of online resources, but the theory alone is not enough. The method for accessing data from ADO is flexible and confusing. most instances on the Internet are based on MFC, and there are many access databases. Here I wrote a C ++ language access Ms SQL2000 instance. I hope it will be helpful to compare with cainiao.
Ii. database creation
First, create a database student in SQL2000 enterprise management and create a table stu_info field and value as follows:
Snum sname sage ssex smajor
200113801 Bin Laden 23 male computer science
200104205 Zhang qiaoqiao 25 female Tourism Management
200113802 Zhang Xueyou 26 male computer science
Iii. Access
The program list is as follows: (Win2000 + vc6.0)
/*************************************** ****************************
Use ADO to access Ms SQL2000
Requirement: [1] output each record in the stu_info table
[2] Add a new record
[3] delete records named "bin Laden"
*/
# Import "C:/program files/common files/system/ADO/msado15.dll "/
No_namespace Rename ("EOF", "endoffile ")
# Include <iostream>
# Include <iomanip> // For SETW ()
Using namespace STD;
Class Stu
{
Public:
Char snum [10]; // student ID
Char sname [10]; // name
Char ssex [2]; // name
Long Sage; // age
Char smajor [20]; // professional
Public:
Stu (){}
~ Stu (){}
};
Int main ()
{
Stu student;
: Coinitialize (null); // initialize the OLE/COM library environment to prepare for accessing the ADO Interface
_ Recordsetptr m_precordset ("ADODB. recordset ");
_ Connectionptr m_pconnection ("ADODB. Connection ");
_ Bstr_t bstrsql ("select * From stu_info"); // query statement
Char * query_cmd = "delete from stu_info where sname = 'binad '";
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 = (local); database = student; uid = sa; Pwd = 123 ;";
// If the database is on the network, the server is in the form of (192.168.1.5, 3340)
// User SA and password 123 are only for my library
M_pconnection-> open (strconnect, "", "", admodeunknown );
If (m_pconnection = NULL)
Cerr <"Lind data error! /N ";
// 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 );
_ Variant_t vsnum, vsname, vsage, vssex, vsmajor; // snum, sname, sage, ssex, and smajor in the corresponding Library
Cout <"student ID, age, and surname ";
Cout <"/n ----------------------------------------------------------------/N ";
While (! M_precordset-> endoffile)
{
Vsnum = m_precordset-> getcollect (_ variant_t (long) 0); // you can specify both the field number and field name here.
Vsname = m_precordset-> getcollect ("sname ");
Vsage = m_precordset-> getcollect ("Sage ");
Vssex = m_precordset-> getcollect ("ssex ");
Vsmajor = m_precordset-> getcollect ("smajor ");
If (vsnum. VT! = Vt_null & vsname. VT! = Vt_null & vsage. VT! = Vt_null
& Vssex. VT! = Vt_null & vsmajor. VT! = Vt_null)
{
Cout. SETF (IOs: Left );
Cout <SETW (14) <(char *) (_ bstr_t) vsnum;
Cout <SETW (14) <(char *) (_ bstr_t) vsname;
Cout <SETW (8) <vsage. lval;
Cout <SETW (8) <(char *) (_ bstr_t) vssex;
Cout <SETW (20) <(char *) (_ bstr_t) vsmajor;
Cout. unsetf (IOs: Left );
Cout <Endl;
}
M_precordset-> movenext (); // move to the next record
}
Cout <"/n ----------------------------------------------------------------/N ";
Cout <"/n enter the student information you want to add/N ";
Cout <"student ID :";
Cin> Student. snum;
Cout <"/n name :";
Cin> Student. sname;
Cout <"/n age :";
Cin> Student. Sage;
Cout <"/n surname :";
Cin> Student. ssex;
Cout <"/n Major :";
Cin> Student. smajor;
M_precordset-> movefirst (); // move to the first record
M_precordset-> addnew (); // Add a new record
M_precordset-> putcollect ("snum", _ variant_t (student. snum ));
M_precordset-> putcollect ("sname", _ variant_t (student. sname ));
M_precordset-> putcollect ("Sage", _ variant_t (student. Sage ));
M_precordset-> putcollect ("ssex", _ variant_t (student. ssex ));
M_precordset-> putcollect ("smajor", _ variant_t (student. smajor ));
M_precordset-> Update ();
M_pconnection-> execute (query_cmd, null, 1); // Execute SQL statement to delete
M_precordset-> close (); // close the record set
}
// Catch exceptions
Catch (_ com_error E)
{
// Display the error message
Cerr <"/nerror:" <(char *) E. Description (); // throw an exception
}
If (m_pconnection-> state)
M_pconnection-> close ();
: Couninitialize ();
Return 0;
}