Sunteți pe pagina 1din 4

Tutorial: Using TSQLMonitor with an ODBC Connection - RAD Studio

http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Using_T...

Show: Delphi C++ Display Preferences

Tutorial: Using TSQLMonitor with an ODBC Connection


From RAD Studio Go Up to Tutorials#SQLite and SQLMonitor In RAD Studio XE3, TSQLMonitor offers support for ODBC connections. The following tutorial shows how TSQLMonitor can be used with an ODBC connection. The ODBC establishes a connection to an Interbase database. The example is a VCL Form Applications that contains 3 controls: TSQLConnection, TSQLMonitor, and TButton. When the button is clicked, a table is created and populated with data, and then a simple select query is executed. Finally, the TraceList of the TSQLMonitor is saved to a file on disk.

Contents
1 Steps 1.1 Create the connection to the ODBC datasource 1.2 The application 2 See Also

Steps
Create the connection to the ODBC datasource
1. Create an ODBC datasource. For more information, see: Microsoft documentation (http://msdn.microsoft.com/en-us/library/ca6axakh(v=vs.80).aspx) . 2. From the Data Explorer, right-click Odbc, and select Add New Connection.

1 of 4

12/23/2013 5:13 PM

Tutorial: Using TSQLMonitor with an ODBC Connection - RAD Studio

http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Using_T...

3. Select a name for the new connection and click OK. In this example, we will use ODBCINTERBASECONNECTION. 4. Right-click the newly created connection, and select Modify. Set the Database field to the name of the Datasource created with ODBC Data Source Administrator (http://msdn.microsoft.com/en-us/library /windows/desktop/ms714024(v=vs.85).aspx) , and the credentials needed to connect to the database.

5. Click Test Connection. If the connections has been properly set, a dialog box stating Test connection succeeded should be displayed.

The application
1. Select File > New > VCL Forms Application - Delphi. 2. Add the following controls the form: a TSQLConnection, a TSQLMonitor, and a TButton. 3. Add the following code to the OnClick event handler of the TButton control:

2 of 4

12/23/2013 5:13 PM

Tutorial: Using TSQLMonitor with an ODBC Connection - RAD Studio

http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Using_T...

procedure TForm2.Button1Click(Sender: TObject); begin try Connect; SQLMonitor1.SQLConnection := SQLConnection1; // Activate the Monitor SQLMonitor1.Active := True; ExecuteQueries; // Write the trace list of the TSQLMonitor to a file on disk SQLMonitor1.SaveToFile('D:\\Log.txt'); except on E: Exception do ShowMessage('Exception raised with message: ' + E.Message); end; end;

Here is the code for the functions called in the OnClick event handler of the TButton control:
// establishes the connection to the ODBC datasource procedure TForm2.Connect; begin SQLConnection1 := TSQLConnection.Create(nil); // the name of the connection created in the data explorer SQLConnection1.ConnectionName := 'odbcinterbaseconnection'; SQLConnection1.LoginPrompt := False; SQLConnection1.LoadParamsOnConnect := True; SQLConnection1.Connected := True; end; // Executes several queries on the database. procedure TForm2.ExecuteQueries; var Query: String; begin try if SQLConnection1.Connected then begin Query := 'CREATE TABLE ExampleTable(id INTEGER, name VARCHAR(50))'; SQLConnection1.Execute(Query, nil); Query := 'INSERT INTO ExampleTable VALUES(1,''test1'')'; SQLConnection1.Execute(Query, nil); Query := 'INSERT INTO ExampleTable VALUES(2,''test2'')'; SQLConnection1.Execute(Query, nil); Query := 'INSERT INTO ExampleTable VALUES(3,''test3'')'; SQLConnection1.Execute(Query, nil); Query := 'SELECT * FROM ExampleTable'; SQLConnection1.Execute(Query, nil); end; except on E: Exception do ShowMessage('Exception raised with message: ' + E.Message); end; end;

See Also
Tutorial: TSQLMonitor support for SQLite databases Data.SqlExpr.TSQLMonitor

3 of 4

12/23/2013 5:13 PM

Tutorial: Using TSQLMonitor with an ODBC Connection - RAD Studio

http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Using_T...

Data.SqlExpr.TSQLConnection FMX.Controls.TButton Data.SqlExpr.TSQLMonitor.TraceList Object Inspector Data.SqlExpr.TSQLConnection.DriverName Data.SqlExpr.TSQLMonitor.SQLConnection Vcl.Controls.TControl.Caption Vcl.StdCtrls.TButton.OnClick Data.SqlExpr.TSQLConnection.Execute Data.SqlExpr.TSQLConnection.Connected Data.SqlExpr.TSQLMonitor.Active Data.SqlExpr.TSQLMonitor.SaveToFile Retrieved from "http://docwiki.embarcadero.com/RADStudio/XE5/e /index.php?title=Tutorial:_Using_TSQLMonitor_with_an_ODBC_Connection&oldid=212426" This page was last modified on 25 October 2013, at 17:59. Help Feedback

4 of 4

12/23/2013 5:13 PM

S-ar putea să vă placă și