[ Pobierz całość w formacie PDF ]
.At design-time, select the desired value forCommandType from the drop-down list in the Object Inspector.At runtime, assign avalue of type TCommandType.ADODataSet1->CommandType = cmdText;Activate the TADODataSet by calling its Open method or by assigning a value of trueto the Active property.ADODataSet1->Connection = ADOConnection1;ADODataSet1->CommandType = cmdText;ADODataSet1->CommandText = "SELECT * FROM Customer";ADODataSet1->DataSource = DataSource1;ADODataSet1->Open();Using TADOTableThe TADOTable component provides C++Builder applications the ability to accessdata from a single table in a database accessed via ADO.The table accessed isspecified in the TableName property of the ADO table component.The database is accessed using a data store connection established by the ADO tablecomponent using its ConnectionString property or through a separate TADOConnectioncomponent specified in the Connection property.See Connecting to a data store usingADO dataset components on page 24-13 for more information on this.Using data provided by a TADOTable component in visual controls, navigatingthrough the rows, and programmatically modifying the data is the same as for therest of the ADO dataset components.See Features common to all ADO datasetcomponents on page 24-11 for more information on features common to all datasetcomponents.Specifying the table to useOnce a TADOTable component has a valid connection to a database, it can accesstables contained in that database.Specify a single table of the database in theTableName property.When the ADO table component is activated, the table and itsdata become accessible through the TADOTable.At design-time, if the TADOTable component has a valid data store connection, theproperty editor for the TableName property lists the names of available tables.Selectone table from this list.At runtime, assign a AnsiString value containing a table nameto the TableName property.ADOTable1->TableName = "Orders";Wor k i ng wi t h ADO c omponent s 24-19Us i n g ADO d a t a s e t sIf a TADOConnection component is used to connect to the data store, you can use itsGetTableNames method to retrieve a list of available tables.GetTableNames fills analready-existing string list object with the names of the tables available through theconnection.void __fastcall TForm1::ListBox1DblClick(TObject *Sender){ADOConnection->GetTableNames(ListBox1->Items, false);}void __fastcall TForm1::Button1Click(TObject *Sender){ADOTable1->Close();Table1->TableName = ListBox1->Items->Strings[ListBox1->ItemIndex];Table1->Open();}Using TADOQueryThe TADOQuery component provides C++Builder applications the ability to accessdata from one or multiple tables from an ADO database using SQL.Specify the SQLstatement to use with the ADO query component in the SQL property.TADOQuerycan either retrieve data using data manipulation language (DML) or create and deletemetadata objects using data definition language (DDL).The SQL used in aTADOQuery component must be acceptable to the ADO driver in use.C++Builderperforms no evaluation of the SQL and does not execute it.The SQL statement ismerely passed to the database back-end for execution.If the SQL statement producesa result set, it is passed from the database back-end through C++Builder to theTADOQuery for use by the application.The database is accessed using a data store connection established by the ADO querycomponent using its ConnectionString property or through a separateTADOConnection component specified in the Connection property.See Connecting toa data store using ADO dataset components on page 24-13 for more information onthis.Using data provided by a TADOQuery component in visual controls, navigatingthrough the rows, and programmatically modifying the data is the same as for therest of the ADO dataset components.See Features common to all ADO datasetcomponents on page 24-11 for more information on features common to all datasetcomponents.Specifying SQL statementsAt design-time, invoke the property editor for the SQL property by clicking theellipsis button in the Object Inspector.In the editor dialog, enter the SQL statementfor the TADOQuery.At runtime, assign a value to the SQL property.As is the case with the standardquerying component, TQuery, the TADOQuery::SQL property is a string list object.Use properties and methods of the string list class to assign values to the SQLproperty.24-20 Dev el oper s Gui deUs i n g ADO d a t a s e t sIn the example below, a SELECT statement is assigned to the SQL property of aTADOQuery component named ADOQuery5.ADOQuery5->Close();ADOQuery5->SQL->Clear();ADOQuery5->SQL->Add("SELECT Company, State");ADOQuery5->SQL->Add("FROM CUSTOMER");ADOQuery5->SQL->Add("WHERE State = " + QuotedStr("HI"));ADOQuery5->SQL->Add("ORDER BY Company");ADOQuery5->Open();Executing SQL statementsA TADOQuery with a valid SQL statement in its SQL property can be executed in oneof two ways.Which way is you use is predicated on the type of SQL statement theADO query component is to execute.If the SQL statement is one that returns a result set, the ADO query componentshould be activated by calling its Open method or by settings its Active property totrue.Only SELECT statements return result sets, so a TADOQuery with a SELECTstatement in its SQL property will always be activated using this approach.ADOQuery1->SQL->Text = "SELECT * FROM TrafficViolations";ADOQuery1->Open();Note that because methods cannot be called while designing an application in theC++Builder IDE, only the Active property can be used to activate this kind of query atdesign-time.This is functionally the same as calling the Open method (at runtime).Execute an SQL statement that does not return a result set by calling the ExecSQLmethod of the TADOQuery component.All SQL statements except SELECT fall intothis category: INSERT, DELETE, UPDATE, CREATE INDEX, ALTER TABLE, and soon.A TADOQuery component with on of these SQL statements in its SQL propertywill always be activated using this approach.ADOQuery1->SQL->Text = "DELETE FROM TrafficViolations WHERE (TicketID = 1099)";ADOQuery1->ExecSQL();The TADOCommand component can be used to execute SQL statements like the oneabove that do not return result sets.Using TADOStoredProcThe TADOStoredProc component provides C++Builder applications the ability toexecute stored procedures in a database accessed through an ADO data store.Thestored procedure executed is specified in the ProcedureName property of the ADOstored procedure component.The database is accessed using a data store connection established by the storedprocedure component using its ConnectionString property or through a separateTADOConnection component specified in the Connection property.See Connecting toa data store using ADO dataset components on page 24-13 for more information onthis.Wor k i ng wi t h ADO c omponent s 24-21Us i n g ADO d a t a s e t sResult sets retrieved by a TADOStoredProc component are made available to anapplication in the same manner as the standard, BDE-centric query componentTStoredProc.Use the ADO stored procedure component for the DataSet property of astandard TDataSource component [ Pobierz całość w formacie PDF ]