Einzelnen Beitrag anzeigen

Furtbichler
(Gast)

n/a Beiträge
 
#3

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 15:28
The only part being questionable is 'how do I know if a database exist?'.
Doing the same for tables (do they exist?) is easy as it is a built in function in the TADOConnection-component ('GetTableNames'). But, you need to connect to the database first in order to be able to use the TADOConnection.

For MSSQL it's rather simple:
1. Connect to the master-DB of the server.
2. execute SELECT * FROM master..sysdatabases where name = 'YourDatabase' to check for the existence of the DB.
3. if it does not exists, create it using the 'CREATE DATABASE' command
4. connect to the database
5. use 'GetTableNames' to find out if the tables you need exist.
6. create them using the 'CREATE TABLE' command.

It is very easy, once you've learned how to use google.

Another very simple approach for SQL-Server is to create an empty database file and tell the connection to use it. In order to do that, simply start SSMS, create the database and the tables and detatch the DB. The file is now ready to use.

For other RDBMS there are other approaches for points 1-3.
  Mit Zitat antworten Zitat