SQL Study Notes: Simple VC Score Management source code

Source: Internet
Author: User

I have read some access database knowledge before I learned it. Looking back at the SQL interfaces for a long time, I feel that a lot of code is actually repeated. (So don't be afraid of databases. In fact, we just need to know a little bit about what we can use !)

 

I like to use "instances" to speak, not powerful ....

 

So there is a demo test program:

 

1. program running effect:

 

2. oninitdialog () initialization Function

/*************************************** *******************************/<Br/> /* function functions: the default initialization function of the dialog box connects to the SQL database and adds the list header <br/>/* function parameter: None <br/>/* return value: True is returned if the return value is successful, otherwise, false is returned. <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> bool cqdlg:: oninitdialog () <br/>{< br/> cdialog: oninitdialog (); <br/> ...... <br/> seticon (m_hicon, true); // s Et big icon <br/> seticon (m_hicon, false); // set small icon </P> <p> // todo: add extra initialization here <br/> // initialize the COM interface <br/> If (! Afxoleinit () <br/>{< br/> afxmessagebox ("failed to initialize COM interface"); <br/>}< br/> hresult hr; <br/> try <br/> {<br/> // create a connection object <br/> hR = m_pconnection.createinstance ("ADODB. connection "); <br/> If (succeeded (HR )) <br/> {<br/> // here, modify the SQL Server IP address, logon user, and password <br/> // you can also change the IP address to an Internet IP address, or free Domain Name <br/> hR = m_pconnection-> open ("provider = sqloledb; server = 192.168.1.100, 1433; database = student; uid = sa; Pwd = manager ", "", "", admodeunknown); <BR/>}< br/> catch (_ com_error e) <br/>{< br/> // catch an exception <br/> cstring errormessage; <br/> errormessage. format ("failed to connect to the database! /R/n error message: % s ", E. errormessage (); <br/> afxmessagebox (errormessage ); /// Display error message <br/>}</P> <p> // the upper and lower list controls initialize the XP style <br/> long lstyle = m_linelist.sendmessage (lvm_getextendedlistviewstyle ); <br/> lstyle | = lvs_ex_checkboxes | lvs_ex_fullrowselect | lvs_ex_gridlines; <br/> m_linelist.sendmessage (lvm_setextendedlistviewstyle, 0, (lparam) lstyle ); </P> <p> // Add the ListBox list header <br/> m_linelist.insertcolumn (0, "name", null, 80,-1 ); <br/> m_linelist.insertcolumn (1, "class", null, 80,-1); <br/> m_linelist.insertcolumn (2, "", null, 80,-1 ); <br/> m_linelist.insertcolumn (3, "Mathematics", null, 80,-1); <br/> m_linelist.insertcolumn (4, "English", null, 80,-1 ); </P> <p> // modify the dialog box title <br/> cstring strtittle; <br/> strtittle = "SQL Demo" + gethostname () + "" + getipaddress (); <br/> setwindowtext (strtittle); </P> <p> return true; // return true unless you set the focus to a control <br/>}

 

3. Read

/*************************************** *******************************/<Br/> /* function functions: read the SQL database record and write the content to ListBox. <br/>/* function parameter: None <br/>/* return value: None <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> void cqdlg:: onbtnread () <br/>{< br/> // todo: add your control notification handler code here <br/> // clear the list box <br/> m_linelist.dele Teallitems (); </P> <p> int nitem; <br/> _ variant_t vname, vbanji, vyuwen, vshuxue, vyinwen; <br/> try <br/> {<br/> // connect to the database and open the maininfo table <br/> m_precordset.createinstance ("ADODB. recordset "); <br/> m_precordset-> open (" select * From db_info ", _ variant_t (idispatch *) <br/> m_pconnection, true), adopenstatic, adlockoptimistic, adcmdtext); </P> <p> // judge whether the record set pointer flag has been read. <br/> while (! M_precordset-> adoeof) <br/>{< br/> // obtain the record set <br/> vname = m_precordset-> getcollect ("name "); <br/> vbanji = m_precordset-> getcollect ("class"); <br/> vyuwen = m_precordset-> getcollect (" "); <br/> vshuxue = m_precordset-> getcollect ("Mathematics"); <br/> vyinwen = m_precordset-> getcollect ("English "); </P> <p> // update the above ListBox <br/> nitem = m_linelist.insertitem (0 xFFFF, (_ bstr_t) vname); <br/> m_linelist.setitem (nitem,, (_ bstr_t) vbanji, null, 0); <br/> m_linelist.setitem (nitem, (_ bstr_t) vyuwen, null, 0 ); <br/> m_linelist.setitem (nitem, 3,1, (_ bstr_t) vshuxue, null, 0); <br/> m_linelist.setitem (nitem, (_ bstr_t) vyinwen, null, 0); </P> <p> // move the record set pointer to the next <br/> m_precordset-> movenext (); <br/> sleep (5); <br/>}< br/> catch (_ com_error E) <br/>{< br/> // An error occurred while reading and Retrieving Database records <br/> cstring strtemp; <br/> strtemp. format ("An error occurred while reading the database. Error Code:" % s "", getlasterror (); <br/> afxmessagebox (strtemp ); <br/>}< br/>}

 

4. Add

/*************************************** *******************************/<Br/> /* function functions: read the SQL database and add a record set <br/>/* function parameter: None <br/>/* return value: None <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> void cqdlg:: onbtnadd () <br/>{< br/> // todo: add your control notification handler code here <br/> try <br/>{< br/> updatedata (true); <br/> Cstring SQL; <br/> m_precordset.createinstance ("ADODB. recordset "); <br/> m_precordset-> open (" select * From db_info ", _ variant_t (idispatch *) <br/> m_pconnection, true), adopenstatic, adlockbatchoptimistic, adcmdtext); <br/> SQL. format ("insert into db_info ([name], [Class], [language], [mathematics], [English]) values ('% s',' % s ', '% d',' % d', '% D') ", <br/> m_name, m_class, m_yuwen, m_shuxue, m_english ); </P> <p> // execute the SQL statement <br/> m_pconnection-> Exec Ute (_ bstr_t) SQL, null, adexecutenorecords); <br/> onbtnread (); <br/> MessageBox ("added successfully! "," Prompt "); <br/>}< br/> catch (_ com_error E) <br/>{< br/> // Add error <br/> cstring errormessage; <br/> errormessage. format ("failed to add. Please check whether the name" % s "already exists! /N error message: "% s" ", m_name, E. errormessage (); <br/> afxmessagebox (errormessage); <br/>}< br/>}

 

5. Modify

/*************************************** *******************************/<Br/> /* function functions: read the SQL database and modify the record set <br/>/* function parameters: None <br/>/* return value: None <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> void cqdlg:: onbtnedit () <br/>{< br/> // todo: add your control notification handler code here <br/> try <br/>{< br/> // modify the data, with the name as the key <br/> UPD Atedata (true); <br/> cstring SQL; <br/> m_precordset.createinstance ("ADODB. recordset "); <br/> m_precordset-> open (" select * From db_info ", _ variant_t (idispatch *) <br/> m_pconnection, true), adopenstatic, adlockbatchoptimistic, adcmdtext); <br/> SQL. format ("Update db_info set [name] = '% s', [Class] =' % s', [language] = '% d ', [mathematics] = '% d', [English] =' % d' where [name] = '% S' ", <br/> m_name, m_class, m_yuwen, m_shuxue, m_english, m_oldname); </P> <p>/ /Execute the SQL statement <br/> m_pconnection-> execute (_ bstr_t) SQL, null, adexecutenorecords ); </P> <p> // refresh and re-read the database <br/> onbtnread (); <br/> MessageBox ("modified successfully! "," Prompt "); <br/>}< br/> catch (_ com_error E) <br/>{< br/> // modification failed <br/> cstring errormessage; <br/> errormessage. format ("failed to modify, error message:" % s "", E. errormessage (); <br/> afxmessagebox (errormessage); <br/>}< br/>}

 

6. Delete

/*************************************** *******************************/<Br/> /* function functions: read the SQL database and delete the record set <br/>/* function parameter: None <br/>/* return value: None <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> void cqdlg:: onbtndel () <br/>{< br/> // todo: add your control notification handler code here <br/> try <br/>{< br/> updatedata (true); <br/> Cstring SQL; <br/> m_precordset.createinstance ("ADODB. recordset "); <br/> m_precordset-> open (" select * From db_info ", _ variant_t (idispatch *) <br/> m_pconnection, true), adopenstatic, adlockbatchoptimistic, adcmdtext); </P> <p> updatedata (true); <br/> SQL. format ("delete from db_info where [name] = '% S'", m_name ); </P> <p> // execute an SQL statement <br/> m_pconnection-> execute (_ bstr_t) SQL, null, adexecutenorecords ); </P> <p> onbtnread (); <Br/> MessageBox ("deleted successfully! "," Prompt "); <br/>}< br/> catch (_ com_error e) <br/>{< br/> cstring errormessage; <br/> errormessage. format ("deletion failed, error message:" % s "", E. errormessage (); <br/> afxmessagebox (errormessage); <br/>}< br/>}

 

7. Read selected row data

/*************************************** *******************************/<Br/> /* function functions: show selected row data <br/>/* function parameter: Default <br/>/* return value: None <br/>/* description: by Koma edit <br/> /****************************** **************************************** **/<br/> void cqdlg:: onclicklist1 (nmhdr * pnmhdr, lresult * presult) <br/> {<br/> // todo: add your control notification handler code here <br/> // obtain the selected <Br/> intnselrows = m_linelist.getselectedcount (); </P> <p> // if not selected, return <br/> If (! Nselrows) <br/> return; <br/> // if no item is selected, null is returned. <br/> positionps = m_linelist.getfirstselecteditemposition (); </P> <p> // return the index value of the next item selected in the List View Control <br/> Inti = m_linelist.getnextselecteditem (PS ); <br/> if (I! =-1) <br/>{< br/> // obtain the information of the selected row <br/> m_name = m_linelist.getitemtext (I, 0 ); <br/> m_class = m_linelist.getitemtext (I, 1); </P> <p> // The Int type variable is associated, the obtained cstring is <br/> // so you need to convert the cstring to int <br/> m_yuwen = atoi (m_linelist.getitemtext (I, 2 )); <br/> m_shuxue = atoi (m_linelist.getitemtext (I, 3); <br/> m_english = atoi (m_linelist.getitemtext (I, 4 )); </P> <p> // transmit data to the control using false <br/> updatedata (false ); <br/>}</P> <p> // save the old name for modification. <br/> m_oldname = m_name; </P> <p> * presult = 0; <br/>}

 

8. Obtain the Host Name and Internet IP address (cstring type)

/*************************************** *******************************/<Br/> /* function Description: obtain the current Internet IP address <br/>/* parameter: None <br/>/* return value: Return Internet IP address, cstring type string <br/>/*: koma 2009.08.21 <br/> /******************************** **************************************** /<br/> cstring cqdlg:: getipaddress () <br/>{< br/> char Buf [max_path] ={ 0 }; <br/> char chtempip [128]; <br/> char chip [64]; <br/> cstring strpat H; <br/> strpath = getexepath () + "// netip. ini "; <br/> urldownloadtofile (0," http://www.ip138.com/ip2city.asp ", strpath, 0, null); </P> <p> file * fp = fopen (strpath, "R"); <br/> If (FP! = NULL) <br/>{< br/> fseek (FP, 0, seek_set); <br/> fread (BUF, 1,256, FP ); <br/> fclose (FP); <br/> char * iindex = strstr (BUF, "["); <br/> If (iindex) <br/>{< br/> sprintf (chtempip, "% s", iindex); <br/> int nbuflen = strlen (chtempip ); <br/> for (INT I = 0; I <nbuflen; I ++) <br/> {<br/> chip [I] = chtempip [I + 1]; </P> <p> If (chtempip [I] = ']') <br/> {<br/> chip [I-1] = '/0 '; <br/> deletefile (strpath); <br/> con Tinue; <br/>}< br/> cstring strtemp = ""; <br/> If (chip! = "") <Br/>{< br/> strtemp. format ("% s", Chip); <br/>}< br/> return strtemp; <br/>}</P> <p> /*************************** **************************************** * *****/<br/>/* function description: get the current host name <br/>/* parameter: None <br/>/* return value: Return host name, cstring type string <br/>/*: koma 2009.08.21 <br/> /******************************** **************************************** /<br/> cstring cqdlg:: gethostname () <br/>{< br/> word wversion Requested; <br/> wsadata; <br/> int err; <br/> wversionrequested = makeword (2, 0); <br/> err = wsastartup (wversionrequested, & wsadata); <br/> If (Err! = 0) <br/>{< br/> return err; <br/>}< br/> If (lobyte (wsadata. wversion! = 2) | hibyte (wsadata. wversion )! = 0) <br/>{< br/> wsacleanup (); <br/> return (char) wsavernotsupported; <br/>}</P> <p> charszhostname [256]; <br/> intnretcode; <br/> cstring STR; </P> <p> // call an API to obtain the host name <br/> nretcode = gethostname (szhostname, sizeof (szhostname); <br/> If (nretcode! = 0) <br/>{< br/> return wsagetlasterror (); <br/>}< br/> Str. format ("Host Name: % s", szhostname); <br/> return STR; <br/>}

 

9, VC + ADO + ms SQL 2000 Source Code download

 

Network Disk download: http://www.rayfile.com/files/051d7080-8e2e-11de-a68f-0014221b798a/

 

All right, other codes will no longer be pasted. Please download the source and check whether the student. MDF database will be imported. Check the previous articles!

 

 

(Ms SQL 2000 learning to this end, have the opportunity to I look at the database of other companies, at least it will not be difficult !)

 

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.