implementing VS2010 Connection SQL Server 2008 operations and programming, the main record is a learning process.
Implementing VS2010 Connectivity SQL Server: a column to tease and fly
Implementing VS2010 Connection SQL Server 2008 code: Click the Open link
Three pointers to ADO:
_CONNECTIONPTR: The connection used to establish the database. _RecordsetPtr: It is a pointer that is specifically designed to operate the database through a recordset, which allows you to perform various operations on records, fields, and so on in the tables of the database. _COMMANDPTR: Committed SQL query string pointer
Objective: To connect the database, to connect to the database, read operation. The results are as follows:
Figure: SQL database
Figure: VS2010 Running Results
Required header file #import "C://program files//common files//system//ado//msado15.dll" No_namespace <span style= "font-family: Arial, Helvetica, Sans-serif; >rename ("EOF", "adoeof") Rename ("BOF", "Adobof") </span>
The three variables used _connectionptr m_pconnection; Database _RecordsetPtr M_precordset; Command _commandptr M_pcommand; Recording
Connection database void Ctestdlg::onbnclickedconnect () {//TODO: Add control Notification Handler code here:: CoInitialize (NULL); Initialize the ole/com library environment HRESULT hr = NULL;TRY{HR = M_pconnection.createinstance (_uuidof (Connection));//Create a Connection object instance if (SUCCEEDED ( HR) {m_pconnection->connectionstring = ("Provider = sqloledb.1; Persist Security Info = False; User ID = ARP; password=123456; Initial Catalog = School; Data Source = (local) "), hr = M_pconnection->open (" "," "", "", adconnectunspecified);//Open database if (FAILED (HR)) { AfxMessageBox (_t ("Open failed!"));}} Else{afxmessagebox (_t ("Create Instance of Connection failed!"));}} catch (_com_error e) {CString temp;temp. Format (_t ("Database connection error \ r \ n Error message:%s"), E.errormessage ()); AfxMessageBox (temp);}}
query database void Ctestdlg::onbnclickedadd () {//TODO: This adds a control to the notification handler code try{m_precordset.createinstance ("ADODB. Recordset "); M_precordset->open ("Select Sno, CNO, grade from SC", _variant_t ((idispatch*) m_pconnection, True), adOpenStatic, adLockOptimistic, adCmdText);} catch (_com_error &e) {AfxMessageBox (e.description ());} _variant_t Sno, CNO, grade;try{int num = m_list. GetItemCount (); while (!m_precordset->adoeof) {CString sno= (_bstr_t) (M_precordset->fields->getitem (_ variant_t ("Sno"))->value); CString CNO = (_bstr_t) (M_precordset->fields->getitem (_variant_t ("CNO"))->value); CString grade = (_bstr_t) (M_precordset->fields->getitem (_variant_t ("Grade"))->value); m_list. InsertItem (num, sno); m_list. Setitemtext (num, 1, CNO); m_list. Setitemtext (num, 2, grade); Num++;m_precordset->movenext ();}} catch (_com_error &e) {AfxMessageBox (e.description ());}}
insert data (without parameters) Try{if (!m_precordset->supports (adaddnew)) return;m_precordset->addnew (); m_precordset->fields-> GetItem (_variant_t ("name"))->value=_bstr_t ("Zhao Wei"), M_precordset->fields->getitem (_variant_t ("Gender")), value=_bstr_t ("female"); M_precordset->fields->getitem (_variant_t ("Age"))->value=_variant_t ((short)); m_ Precordset->fields->getitem (_variant_t ("Marry"))->value=_bstr_t ("unmarried"); M_precordset->update (); }//trycatch (_com_error &e) {:: MessageBox (NULL, "something goes wrong.) "," hint ", mb_ok│mb_iconwarning);}
VS2010 connecting SQL Server 2008 operations and programming (note)