Updates information in the database.
Author:Www.gudianxiaoshuo.com
This article is organized by the Code Assistant Software and released content is irrelevant to this software.More comfortable reading, more comfortable writing, and easier Publishing
The show-colored book and Text software is audible, readable, writeable, knowledgeable, and annotated. Coupled with the most powerful pure text color function in history, you can instantly show your book and text.
Method 1: use SQL statements
Update a column
StrCmd. format (_ T ("update classInfo set class ID = % d where ID = % d"), iClassID, iClassID); spCmd-> CommandText = (LPCTSTR) strCmd; spCmd-> Execute (NULL, NULL, ad1_text );
Multiple update columns are separated by commas (,).
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'WHERE LastName = 'Wilson'
Multiple batch updates
Try {CString strCmd; _ CommandPtr spCmd; spCmd. createInstance (_ uuidof (Command); spCmd-> ActiveConnection = m_pConnection; int nCount = random (); CString ruxuenianStr; CString banjiStr; CString xueqiStr; for (int nIndex = 0; nIndex <nCount; nIndex ++) {ruxuenianStr = random (nIndex, 1); banjiStr = m_BanjiList.GetItemText (nIndex, 2); xueqiStr = random (nIndex, 3); strCmd. format (_ T ("update ClassInfo set name = % d where admission year = % d and class = \" % s \ "and semester = \" % s \""), explain (nIndex), _ ttoi (ruxuenianStr), banjiStr, xueqiStr); spCmd-> CommandText = (LPCTSTR) strCmd; spCmd-> Execute (NULL, NULL, ad1_text );}} catch (_ com_error & e) {AfxMessageBox (e. error ());}
// Update the student ID CString strCmd; strCmd in the class table in batches. format (_ T ("SELECT * FROM class Table % d order by student ID"), m_pParentStudentInfo-> m_nClassID); pRecordSet-> Open (_ variant_t) strCmd, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext); {pRecordSet-> MoveLast (); int nNew = 0; while (! PRecordSet-> adoBOF) {varValue = pRecordSet-> GetCollect (_ T ("student ID"); if (varValue. vt! = VT_NULL) {nStuXuehao = _ ttoi (LPCTSTR) _ bstr_t (varValue); // nNew = id_Map [nStuXuehao]; pRecordSet-> PutCollect (_ T ("student ID"), (long)-nStuXuehao); pRecordSet-> MovePrevious () ;}} pRecordSet-> UpdateBatch (adAffectAll );}
Method 2: Use AddNew Update
_ RecordsetPtr pRecordSet; CString strCmd; pRecordSet. createInstance (_ uuidof (Recordset); strCmd. format (_ T ("SELECT * FROM class Table % d where student ID = % d"), \ locate the record to be modified m_pParentStudentInfo-> m_nClassID, nXuehao ); pRecordSet-> Open (_ variant_t) strCmd, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText); // pRecordSet-> AddNew (); add new records using AddNew, modify records, delete this statement pRecordSet-> PutCollect (_ T ("student ID"), (long) nXuehao); pRecordSet-> PutCollect (_ T ("name"), (LPCTSTR) nameStr); pRecordSet-> PutCollect (_ T ("gender"), (LPCTSTR) sexStr); pRecordSet-> PutCollect (_ T ("Address"), (LPCTSTR) addStr); pRecordSet-> PutCollect (_ T ("contact information"), (LPCTSTR) lianxiStr); pRecordSet-> PutCollect (_ T ("Remarks"), (LPCTSTR) beizhuStr); CFile f; CFileException e; if (f. open (zhaoPianStr, CFile: modeRead | CFile: typeBinary, & e) // Open a jpg file {int nSize = f. getLength (); // first obtain the jpg file Length BYTE * pBuffer = new BYTE [nSize]; // apply for a memory on the heap based on the file size if (f. read (pBuffer, nSize)> 0) // Read the jpg file to pBuffer (applying for a block of memory on the stack) {BYTE * pBuf = pBuffer; /// the main section below is to put the jpg data in pBuffer into the database VARIANT varBLOB; SAFEARRAY * psa; SAFEARRAYBOUND rgsabound [1]; if (pBuf) {rgsabound [0]. lLbound = 0; rgsabound [0]. cElements = nSize; psa = SafeArrayCreate (VT_UI1, 1, rgsabound); for (long I = 0; I <(long) nSize; I ++) SafeArrayPutElement (psa, & I, pBuf ++); varBLOB. vt = VT_ARRAY | VT_UI1; varBLOB. parray = psa; pRecordSet-> GetFields ()-> GetItem (_ T ("photo")-> AppendChunk (varBLOB);} delete [] pBuffer; pBuf = NULL ;} f. close () ;}precordset-> Update (); // Update this record pRecordSet-> Close (); // Update this student to strCmd in the curriculum. format (_ T ("SELECT * FROM classroom sequence table % d where student ID = % d"), m_pParentStudentInfo-> m_nXueqiID, nXuehao); pRecordSet-> Open (_ variant_t) strCmd, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext); pRecordSet-> PutCollect (_ T ("student ID"), (long) nXuehao ); pRecordSet-> PutCollect (_ T ("name"), (LPCTSTR) nameStr); pRecordSet-> Update (); _ RecordsetPtr pRecordSet; CString strCmd; pRecordSet. createInstance (_ uuidof (Recordset); // update the student to strCmd in the course list. format (_ T ("SELECT * FROM classroom sequence table % d where student ID = % d"), m_pParentStudentInfo-> m_nXueqiID, nOldXuehao); pRecordSet-> Open (_ variant_t) strCmd, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext); pRecordSet-> PutCollect (_ T ("name"), (LPCTSTR) nameStr); pRecordSet-> Update (); pRecordSet-> Close ();