Access SQL Server 2000 through C ++

Source: Internet
Author: User

First, create a database student in SQL2000 enterprise management, and then create a table stu_info field and value.
Then use ADO to access MS SQL2000
Requirements: 1. output each record in the stu_info table
2. Add a new record
3. delete a record
# Include <iostream>
# Include <iomanip> // setw () is required ()
Using namespace std; // namespace
Class student
{
Public:
Char snum [10]; // student ID
Char sname [10]; // name
Char ssex [2]; // name
Long sage; // age
Char smajor [20]; // professional
Public:
Student (){}
~ Student (){}
};

Int main ()
{
Student;
: CoInitialize (NULL); // initialize the OLE/COM library environment, mainly to prepare for accessing the ADO Interface
_ RecordsetPtr m_pRecordset ("ADODB. Recordset ");
_ ConnectionPtr m_pConnection ("ADODB. Connection ");

_ Bstr_t bstrSQL ("select * from stu_info"); // SQL query statement
Char * query_cmd = "delete from stu_info WHERE sname =Heiji";
Try
{
M_pConnection.CreateInstance ("ADODB. Connection"); // create a Connection object
_ Bstr_t strConnect = "Provider = SQLOLEDB; Server = (local); Database = student; uid = sa; pwd = 888;"; // set the connection string, it must be BSTR or _ bstr_t. If the database is on the network, the Server is like (192.168.1.2, 2222). The user sa and password 888 are my own.
M_pConnection-> Open (strConnect, "", "", adModeUnknown );
If (m_pConnection = NULL)
Cerr <"Lind data ERROR! ";
M_pRecordset.CreateInstance (_ uuidof (Recordset); // create a record set object
M_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (),
AdOpenDynamic, adLockOptimistic, adshorttext); // retrieve records from the table above
_ Variant_t vsnum, vsname, vsage, vssex, vsmajor; // vsnum, sname, sage, ssex, and smajor in the corresponding Library
Cout <"student ID, age, and surname ";
Cout <"----------------------------------------------------------------";
While (! M_pRecordset-> EndOfFile)
{
Vsnum = m_pRecordset-> GetCollect (_ variant_t (long) 0); // you can specify either the field number or 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 (); // transfer to the next record
}
Cout <"----------------------------------------------------------------";
Cout <"Enter the student information you want to add ";
Cout <"student ID :";
Cin> student. snum;
Cout <"name :";
Cin> student. sname;
Cout <"Age :";
Cin> student. sage;
Cout <"name :";
Cin> student. ssex;
Cout <"Major :";
Cin> student. smajor;
M_pRecordset-> MoveFirst (); // transfer 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 statements to delete records
M_pRecordset-> Close (); // closes the record set.
}


Below is an exception capture
Catch (_ com_error e)
{// Display error message
Cerr <"ERROR:" <(char *) e. Description (); // throw an exception
}
If (m_pConnection-> State)
M_pConnection-> Close ();
: CoUninitialize ();
Return 0;
}
I don't know if you can understand it. In fact, it's very easy to do this. If there are any problems in my program, or there is something to be improved to do better, let's discuss it together.

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.