Board index » delphi » How to connect to a database?

How to connect to a database?

My first attempt at database use and I'm pullingmy hair out trying to
load a database I created in the database desktop section.
I created a paradox database, just out of choice and saved it.
Now, when I tell the tdatabase component my path and filename on
creating the form, it keeps causing access violations.
I can use the sample databases specified in the alias's but not
directly to a database file.
I thought it would be this simple, if not why not?
thanks
 

Re:How to connect to a database?


Quote
On Mon, 24 Jan 2000 13:05:04 GMT, awh...@southwest.com.au (Al) wrote:
>My first attempt at database use and I'm pullingmy hair out trying to
>load a database I created in the database desktop section.
>I created a paradox database, just out of choice and saved it.
>Now, when I tell the tdatabase component my path and filename on
>creating the form, it keeps causing access violations.
>I can use the sample databases specified in the alias's but not
>directly to a database file.
>I thought it would be this simple, if not why not?

It is not totally clear what you are doing and where the problem is
occurring. As a result of this lack of clarity, it is impossible to give
you a definitive solution.

First a small clarification. What you created in Database Desktop is a
Paradox *table*. For local table types (Paradox, dBASE, FoxPro), a
"database" is the directory that contains the table files (*.db) and
auxiliary files (*.px, *.xg0, *.mb, etc.). I do not mention this to pick
nits, but because it can be important in Delphi programming. Certain
properties and processes need a reference to a database. Others need a
reference to a table. The two are not interchangeable.

What dataset component are you using to access the table? TTable or TQuery?
To what is its DatabaseName property set? (Provide actual string as this
can be critical.) Is access to the database through a BDE alias (specified
in the DatabaseName property)? Or through a string with the drive and
directory (specified in the DatabaseName property)? In what drive and
directory does the table reside?

What specific exception type is raised? EAccessViolation, EDBEngineError,
EDatabaseError, or ... ? What is the error message?

On execution of what single command line is this exception raised? Use the
integrated de{*word*81} to step through this area of your application to find
the exact line. This is important. If the exception is not raised as a
result of a line of code you wrote, indicate this.

(Follow-ups to the newsgroup only, please.)

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:How to connect to a database?


Have you set up your database correctly in the BDE Administrator?  Forgive
my ignorance if this is not the problem.

Quote
Al <awh...@southwest.com.au> wrote in message

news:388c4d0b.860248@news.southwest.com.au...
Quote
> My first attempt at database use and I'm pullingmy hair out trying to
> load a database I created in the database desktop section.
> I created a paradox database, just out of choice and saved it.
> Now, when I tell the tdatabase component my path and filename on
> creating the form, it keeps causing access violations.
> I can use the sample databases specified in the alias's but not
> directly to a database file.
> I thought it would be this simple, if not why not?
> thanks

Re:How to connect to a database?


On Mon, 24 Jan 2000 20:50:55 GMT, koter...@NOSPAMgte.net (Steve

Quote
Koterski) wrote:

>It is not totally clear what you are doing and where the problem is
>occurring. As a result of this lack of clarity, it is impossible to give
>you a definitive solution.

Yes my apologies, it was rather vague. Written in frustration!
What I did was try to give the Tdatabase component a databasename, now
to me it seemed I had to specify the actual database file name, now I
have found it accepts the directory only. So far so good.
What do I do if I send someone my database utility? If I set the
databasename to the application directory is that going to work with
the file.db file in there?
I really am confused with the help file on alias's, even getting the
neccessary bde files to my friend results in installshield asking for
the alias name. What do I put in there if I only have the table saved
in a directory? I can package the executable ok but unsure on what to
setup the bde section of installshield to. Is there a way of getting
the bde files to another person as a stand alone?
Is it necessary for me to use a tdatabase with paradox or will the
table suffice?
I ask to many questions don't I? Well I know the answer to that one at
least!

Cheers

Re:How to connect to a database?


Quote
On Tue, 25 Jan 2000 08:43:23 GMT, awh...@southwest.com.au (Al) wrote:
>Yes my apologies, it was rather vague. Written in frustration!
>What I did was try to give the Tdatabase component a databasename, now
>to me it seemed I had to specify the actual database file name, now I
>have found it accepts the directory only...

The TDatabase.DatabaseName property is designed to provide a unique
identifier for the TDatabase component within the context of the
application. This property is used to associate dataset components (like
TTable and TQuery components) with the TDatabase. By setting the
DatabaseName property of the TDatabase and TTable components to the same
string, the dataset components use the connection to the database
established by the TDatabase. This can be done at design-time, in the
Object Inspector. Below is how it looks when done programatically at
runtime.

  Database1.DatabaseName := 'AppDb';
  Table1.DatabaseName := Database1.DatabaseName;
  Table1.Open;

To specify the database to which a TDatabase component is to connect, use a
parameter in its Params property. For local database types (dBASE, Paradox,
FoxPro), the "database" is nothing more than a directory on the host
computer or a network server. To specify this directory in the TDatabase
component, use the PATH parameter. Again, possible to do at design-time or
runtime.

  with Database1 do begin
    Connected := False;       { Not mandatory, but safe }
    DriverName := 'STANDARD'; { Driver for Paradox & dBASE }
    DatabaseName := 'AppDb';  { Unique identifier for the TDatabase }
    Params.Clear;             { Clear and set the parameters }
    Params.Add('PATH=c:\Programs\Data\');
  end;
  with Table1 do begin
    DatabaseName := Database1.DatabaseName; { Specify the database }
    TableType := ttDefault;                 { Set the table type }
    TableName := 'File.db';                 { Specify name of table }
    Open;                                   { Activate the TTable }
  end;

Note in the above the importance I alluded to before regarding the
difference between a database and a table. The property settings above are
based on those differences. The history of local databases has muddied the
concepts some over the years. But this is one situation where there can be
no ambiguity.

Quote
>...So far so good.
>What do I do if I send someone my database utility? If I set the
>databasename to the application directory is that going to work with
>the file.db file in there?...

As you can see from the preceding example, you can use a string (a literal
as above or a variable) to represent the local "database" as well as the
table name.

Quote
>...I really am confused with the help file on alias's, even getting the
>neccessary bde files to my friend results in installshield asking for
>the alias name. What do I put in there if I only have the table saved
>in a directory? I can package the executable ok but unsure on what to
>setup the bde section of installshield to. Is there a way of getting
>the bde files to another person as a stand alone?
>Is it necessary for me to use a tdatabase with paradox or will the
>table suffice?

If you represent the database name and table name as strings, there is no
real, pressing need for a BDE alias. Hence no need for headaches trying to
bend InstallShield Express around them. As a string, these values can come
from a number of sources: an INI file, a Windows Registry setting, a dialog
presented to the end-user by your application, or a string programmatically
build by the application.

For example, the TApplication.EXEName property yields the name of the
application's executable file, including drive, directory, and file name.
If you pass that value through the ExtractFilePath function, you drop the
executable name and leave just the drive and directory. To the end of that
you can add the subdirectory in which the application's table files reside.
Use that dynamically built string for the PATH parameter.

  with Database1 do begin
    DriverName := 'STANDARD';
    DatabaseName := 'AppDb';
    Params.Clear;
    Params.Add('PATH=' + ExtractFilePath(Application.EXEName) +
      'DataFiles\');
  end;
  ...

You still need the BDE properly installed. That is the job of
InstallShield. But at least using this approach you do not need to muck
about with BDE aliases.

Of course, if you really *want* to use a BDE alias, one can be created at
runtime. You could, for example, check when your application starts to see
whether your BDE alias exists (for this see the TSession.GetAliasNames
method). If it does not, use the TSession.AddStandardAlias method. As a
variation -- you might do this only once, the first time your application
is run -- as a first-time initialization process.

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:How to connect to a database?


Well that was more than I expected in a reply! Many thanks.
I placed a table on my form and was able to use my database without
the tdatabase, is that normal? Should I still include the database
component even though the util worked without it?
Again, many thanks for your help.
cheers

Re:How to connect to a database?


Quote
On Wed, 26 Jan 2000 03:27:37 GMT, awh...@southwest.com.au (Al) wrote:
>Well that was more than I expected in a reply! Many thanks.
>I placed a table on my form and was able to use my database without
>the tdatabase, is that normal? Should I still include the database
>component even though the util worked without it?

The DatabaseName property of dataset components can be set to one of four
different values:

1. A string containing the name of a BDE alias.

2. The same string as used in the DatabaseName property of a
   TDatabase. (This is what we have been talking about in this
   thread.)

3. A string containing the drive and directory where the table(s)
   reside.

4. A blank string (typically only applies to a TQuery where the
   drive/directory or a BDE alias is specified in the SQL statement
   itself, as part of a table reference).

When dealing with local table types (Paradox, dBASE, FoxPro), it is really
more a matter of personal preference to use or not use a TDatabase for an
application's database connection. You could opt to connect all dataset
components directly to the database (such as through #1 or #3 above). One
benefit use of a TDatabase affords is easier changing of the target
database location. If you do not use a TDatabase, the DatabaseName property
must be changed in *each* dataset component (TTable and TQuery) in the
application. An application could have quite a few dataset components, too.
But if you use a TDatabase, you must only make the change in one place:
that TDatabase. All associated dataset components automatically use the new
directory by virtue of their association with the TDatabase.

All of what has been discussed in this thread is, of course, in the online
help. The chapter Developing Database Applications has a lot of information
covering general database programming. In your specific situation, there
are a few chapters that would be of special interest. In the Delphi help
table of contents navigate through the topic titles:

  Developing Database Applications
    Connecting to databases
    Understanding datasets
    Working with tables

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:How to connect to a database?


On Wed, 26 Jan 2000 16:08:17 GMT, koter...@NOSPAMgte.net (Steve

Quote
Koterski) wrote:
>application. An application could have quite a few dataset components, too.
>But if you use a TDatabase, you must only make the change in one place:
>that TDatabase. All associated dataset components automatically use the new
>directory by virtue of their association with the TDatabase.

Righto, that makes sense now, I wondered why it worked without the
tdatabase but I can see it's like a central hub for easy changes as
you describe.
Thanks a million.
Cheers

Other Threads