This is to give you some ideas -- use at your own risk ;-) . It is not the only
solution. Furthermore, this example does not deal with runtime errors.
First lets deal with the :PRIV alias question:
One thing you can do is create an alias called PRIV in the idapi.cfg file. Use
the BDE Administrator to add the alias. By doing this, you can access the alias
directly via your components at design time. However, you should set the
session's private directory in code before you open any tables at runtime. Use
Session.PrivateDir, otherwise a default private directory will be used by the
application. One place for this code is the Form's OnCreate event:
procedure TForm1.FormCreate(Sender: TObject);
begin
Session.PrivateDir := 'C:\windows\temp';
end;
Next we deal with your first question:
From the component palette select and place each of the following components on
a Form: 1) a Query (Query1), 2) a BatchMove (BatchMove1), 3) a Table
(Table1), 4) a DataSource (DataSource1), 5) a DBGrid (DBGrid1), and lastly 6) a
Button (Button1).
Set the Query1 DatabaseName property to your "MainAlias" (I assume you have
created it in the idapi.cfg file), or you can type in the path to your data.
Next place your SQL statement in the Query1 SQL property, or create the
statements using the built-in SQL Builder (right-click on the SQL component and
select SQL Builder from the pop-up menu).
Now set the Table1 DatabaseName property to the :PRIV alias you created in the
idapi.cfg file, or just type in the private directory path. Also, set Table1's
TableName property to "Result" (without the quotations). This is what the new
answer table will be called. You can name it something else if you like.
Connect DataSource1 to Table1: Set the DataSource1 DataSet property to Table1.
This will allow other components to access the contents of Table1.
Connect the DBGrid1 to DataSource1 so you can see the contents of Table1 at
runtime: Set the DBGrid1 DataSource property to DataSource1.
Set the following BatchMove1 properties: Destination = Table1, Mode = batCopy,
and Source = Query1.
Lastly, place this code in your Button1's OnClick event:
procedure TForm1.Button1Click(Sender: TObject);
begin
Query1.Open;
BatchMove1.Execute;
Table1.Open;
end;
Run the application and click on the button.
Note: The reason I used a BatchMove component is so you can write the contents
of your Query to a physical table. In this case, a table called "Result" will
be created in your :PRIV directory. Otherwise the results of your query will
exist only in memory and will be released when the application terminates.
Erik.
Quote
Simon Keefe wrote:
> I am having problems getting my head around the way Delphi handles database
> connections, as opposed to the way paradox does. Does anyone know how I may
> achieve the following in Delphi 4? The code below is ObjectPAL code from
> Paradox 8:
> var
> sqlvar sql
> sqlstr string
> db database
> endvar
> sqlstr="insert into :priv:tmatters select m.* from :mainalias:matters"
> sqlvar.readfromstring(sqlstr)
> db.open(":work")
> sqlvar.executesql(db)
> db.close()
> I also can't seem to find any reference to the PRIV alias in Delphi, which I
> used to use extensively. The reason I want to do this is that I don't want
> to keep a constant connection to the database, and I treat all data views as
> local, refreshing on a time basis. :mainalias: is an alias to SQL Server,
> and we have found that keeping connections open is detrimental to
> performance and the UI (scrollbars become disproportional etc).
> Can anyone help me here, I'm getting a bit baffled.
> Cheers in advance
> Simon Keefe