Outside data access method studies in UG2 second development

  • Time:
  • Click:233
  • source:TANAY CNC Machining
1 preface is mixed as the promotion of UG system popularize application, the 2 development work that is based on UG platform appears more and more important, in the system of 2 development application in UG, often need with all sorts of outside data contact with, UG/OPEN API offerred rich file to visit operation function, the MFC that uses these operation function and VC++ kind library, can realize outside data to read conveniently write visit process designing. The main method that 2 outside data visit is in system of application of UG2 second development, the outside data of main visit has watch of text file, Excel, relation database the watch, can visit these outside data through the following method normally: (1) the file operation function that provides through UG/OPEN API visits outside data (2) visit outside data through ODBC data source (3) visit outside data through ADO 2.

The function that the 1 document that offers through UG/OPEN API handles function to visit the relevant document in UG/OPEN API of text file data to operate is included in head file Uf_cfi.

In H, through function Uc4500 can open binary file, relevant file operates function to have Uc4510, Uc4511, Uc4512, Uc4513 and Uc4520, Uc4521, Uc4522, Uc4523, through function Uc4504 can open text file, relevant file operates function to have Uc4514, Uc4524, Uc4525, function Uc4540 is used at shutting a file, whether is judgement file put in use function UF_CFI_ask_file_exist. When the document that offers through UG/OPEN API operates function to visit outside data, want to note the following problem: (1) the file in UG/Manager and IMAN and folder operation cannot use Uf_cfi.

The function in H; (2) the to file and folder identifier length in UG/OPEN API is like next limitation: Length cannot exceed file name UF_CFI_MAX_FILE_NAME_LEN; The length of folder method cannot exceed UF_CFI_MAX_PATH_NAME_LEN; The length of whole file method cannot exceed UF_CFI_MAX_PATH_NAME_LEN. Visit the order flow of text file to pursue as follows: 2.

2 through ODBC data source visit Excel is expressed or relation database expresses data ODBC (database of open mode of Open Database Connectivity interrelates) it is a when put forward by Microsoft unified interface level that is used at visitting a database, it offerred a port of consistent applied process process designing, this interface uses a driver that is aimed at a database and different database to work in coordination. When visitting outside data through ODBC, the data source DSN that needs to appoint a visit first (Data Source Name) , enter the mouth in order to assign ODBC and relevant driver relative to what answer. ODBC data source control can be opened in the administrative tool of Control Panel implement, appoint DSN. Build MFC database kind the visit process designing that Cdatabse object can realize pair of outside data. Object of a CDatabase represents a join with data source. After establishing target of a CDatabase, call Open or OpenEx member function to appoint a DSN, the link that established the rock-bottom data source with DSN (open a database) . Member function Close shuts a database. CDatabase object normally with volume of one or more record (CRecordset object) use together, through CRecordset kind each member function finishs what logarithm occupies a source to read keep an operation. The code of process designing example that visits an Excel watch through ODBC is as follows: The CDatabse database that Static Void Do_ugopen_api(void) {CDatabase Database; // defines a MFC kind object Databse CString SQLCommand = "selec `Serial_Number` , `Outer_Diameter` , `Inner_Diameter` , connective of setting of DWORD DwOptions = 0;// of statement of inquiry of `Height` From `Sheet1$`"; // SQL builds data of database of means CDBVariant Temp;// general kind Int Result; // stores the variable Char Buf[133]; Try {Result = Database of return of value.

OpenEx("DSN=Standard Part" , dwOptions); // opens database If(Result! If = 0) // is successful open {CRecordset Rs(&Database); // to define record volume If (Rs.

Open(CRecordset: Snapshot, SQLCommand) ! = False) {Rs.

MoveFirst(); Short NFields = Rs.

GetODBCFieldCount();// gets the field amount UF_UI_open_listing_window(); While(of the record! Rs.

IsEOF() ) {For(Short Index = 1; Index<NFields; Index++ ) {Rs.

GetFieldValue(Index, temp); Sprintf(buf, "%f" , temp.

M_dblVal); UF_UI_write_listing_window(buf); UF_UI_write_listing_window("t" ); } UF_UI_write_listing_window("n" ); Rs.

MoveNext(); } Rs.

Close();// shuts record volume} Database.

Close(); // closes database join} } Catch(CDBException *pe) // gives wrong processing {AfxMessageBox("Exception! ") ; AfxMessageBox(Pe->m_strError); Pe ->Delet(); } } 2.

The 3 database object technologies that visitting outside data ADO(ActiveX Data Objects) through ADO is Microsoft, the face includes the boy or girl friend of join database in ADO, the record volume object that returns data from inside inquiring a statement and the field object that behave data element. The library file that supports ADO process designing is Msado15.

Dll, its acquiesce maintains presence " C:pRogram Filescommon Silessystemado " below catalog, trends links library Msado15.

Dll enclosed all functions of ADO. Below acquiescent circumstance, visual C++ does not support ADO boy or girl friend. Want to use ADO target in the program, command of need use #import guides ADO library file in the project, code is as follows: #import " C:PRogram FilesCommon FilesSystemadomsado15.

Dll " No_namespace Rename(" EOF " , " AdoEOF " ) Rename(" BOF " , " AdoBOF " ) the name space that parameter No_namespace expresses not to use ADO. Conflict to avoid occurrence constant name, need general EOF is incognito for AdoEOF, BOF incognito for AdoBOF. ADO library contains 3 basic port, namely _ConnectionPtr interface, _CommandPtr interface and _RecordsetPtr interface. They part corresponding Connection object, Command object and Recordset object. Through these 3 basic interface can realize the visit to the database. The common move that visits a database through ADO is as follows: (1) receive data source repeatedly; (2) the order that assigns visit data source (inquiry, newer, delete wait for an operation to command) ; (3) executive command. About ADO library the use method of basic interface refers to relevant reference manual please, the example program code that gives out to visit Access database to express through ADO below paragraph: Initialization of Bool DoAdoTest(Void) {// is right of COM call CoInitialize(0); _ConnectionPtr PConnection = NULL; // to join _RecordsetPtr PRecordset = NULL; // records collect HRESULT Hr = E_UNEXPECTED; CString StrConnectionString("Provider=Microsoft.

Jet.

OLEDB.

4.

0;Data falls Source=f:uGchapter3_4screw.

PConnection of statement of CString StrSqlCommandString("selec * From Screw_table " ); // SQL of string of join of Mdb" ); // .

CreateInstance(__uuidof(Connection) ); // ("ADODB.

Connection" ); PRecordset.

CreateInstance(__uuidof(Recordset) ); // ("ADODB.

Recordset" ); Try {Hr = PConnection->Open((_bstr_t)strConnectionString, "" , "" , messageBox(NULL of database of join of AdModeUnknown); // , "Join database is successful! ", "Clew " , MB_OK | MB_IConINFORMATION); } Catch (_com_error E) {MessageBox(NULL, "Join database fails! ", "Wrong " , MB_OK | MB_IConERROR); Return False; } Try {Hr = PRecordset->Open((_bstr_t)strSqlCommandString, _variant_t((IDispatch*)pConnection, true) , adOpenStatic, adLockOptimistic, adCmdText); // gets record volume} Catch (_com_error* E) {E->ErrorMessage(); MessageBox(NULL, "Open record volume failure! ", "Wrong " , MB_OK | MB_IConERROR); } CString Tmp; UF_UI_open_listing_window(); // outputs field name Long LFieldsCount = -1; LFieldsCount = PRecordset->GetFields()->Count; For (Long I = 0; I<LFieldsCount; I++ ) {Tmp.

Format("%-7s" , (LPCSTR)pRecordset->GetFields()->Item[I]->Name); UF_UI_write_listing_window((LPTSTR)(LPCSTR)tmp); UF_UI_write_listing_window("t" ); } UF_UI_write_listing_window("nn" ); // outputs all record PRecordset->MoveFirst(); While(! PRecordset->adoEOF) {Tmp = PRecordset->GetCollect(0L); UF_UI_write_listing_window((LPTSTR)(LPCSTR)tmp); ... / / in outputting a record, conclusion of PRecordset->MoveNext(); } PRecordset->Close(); PRecordset = NULL; PConnection->Close(); PConnection = NULL; CoUninitialize(); Return True; } 3 of other field value is in process of UG2 second development, choose which kinds of method to visit outside data certainly, depend on specific applied system requirement. Through UG/OPEN API process designing relatively agree with the visit of pair of exterior text files, can go to the lavatory the function such as implementation user and each other of oral sex of UG information window; Through ODBC and ADO the technology can visit the data source of all sorts of support, include watch of text file, Excel and all sorts of relations database the watch, ADO and ODBC photograph are compared, use more handy, discharged work of trival configuration data source, but some databases support ODBC only, do not support ADO database technology directly. CNC Milling CNC Machining