Connecting visual c ++ and SQL Server 2000 database to the ADO background

Source: Internet
Author: User
Now, I want to connect to the SQL database with vc6.0 in the background. For more information, see Chapter 1 of Visual C ++ common database module development and system migration. The following describes the procedure:

1. Create a dialog box-based program named data.

2. Open SQL Server and create a new data1 database.

3. Enter # import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" no_namespace Rename ("EOF", "adoeof") in the stdafx. h file ")

Do not include semicolons.

4. In the automatically generated cdataapp class source file, that is, before the data. cpp source file, two global variables are defined:

# Endif _ connectionptr m_pconnection; // Add it here. Remember _ recordsetptr m_precordset;

It is defined in the header file of the generated application, but I tried. when defined in H, errors will occur when the cdatadlg class accesses these two variables, so I changed it to data. CPP is defined earlier, and then in cdatadlg. the CPP source file can be used with the extern declaration.

5. Add the COM Initialization code to the Data. cpp initialization function:

Bool cdataapp: initinstance () {If (! Afxoleinit () {afxmessagebox ("OLE initialization error! "); Return false;} afxenablecontrolcontainer (); // initialize the COM Object coinitialize (null );
         ......
Then, add the following code to create a table named student information:
Try {m_pconnection.createinstance ("ADODB. connection "); //" ADODB. connection "_ uuidof (connection) m_precordset.createinstance (" ADODB. recordset "); // defines the database connection string, m_pconnection-> open (" driver = {SQL Server}; server = (local); database = data1 ","", "", admodeunknown );//!!!!!!!!!
        }
// Get a com error. If an error occurs while executing the com function, you can catch the _ com_error exception catch (_ com_error & E) {: couninitialize ();:: afxmessagebox (E. errormessage (); Return false ;}
Comment !!!!!!! Note that if you do not set a password when installing SQL, UID and PWD should be canceled here. If you have a password, follow the following method, I used the following method before. During debugging, idispatch #3149 always appeared. Then I removed the two and it was okay.
m_pConnection->Open("driver={SQL Server};Server=(local);DATABASE=data1;UID=sa;PWD=000","","",adModeUnknown);
6. in the resource List dialog box, add a button and add the corresponding function. In this function, add the following code to create a table and add a record and the number of returned records:
Void cdatadlg: onbtntable () {// todo: add your control notification handler code here_variant_t RA; m_pconnection-> execute ("create table student information (student ID integer, Name text, age integer, birthday datetime) ", & RA, ad1_text); // Add a record to the table, m_pconnection-> execute (" insert into student information (student ID, name, age, birthday) values (200202, 'zhou hua', 22, '2017-08-16 ') ", & RA, ad1_text ); // execute the SQL statistics command m_precordset = m_pconnection-> execute ("select count (*) from student information", & RA, ad1_text ); _ variant_t vcount = m_precordset-> getcollect (_ variant_t) (long) (0); m_precordset-> close (); cstring message; message. format ("% d records in total", vcount. lval); afxmessagebox (Message );}
After editing and running, you can see that the database data1 has a table with student information and a record.
 
I tried again just now. In fact, there is no need to define the following two variables in data. cpp:
_ConnectionPtr m_pConnection;_RecordsetPtr m_pRecordset;
You can directly define the COM object before datadlg. cpp and move it to the initialization function of datadlg. cpp. You can try again. I am just a newbie. Please advise me more about the shortcomings.

Conclusion: It is not as difficult as you think about the content in the book.

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.