Creating a database in SQL code is a little different. You can use the Data Builder to create a new database, or you can do it direct from code.
The create database command is the only SQL command that does not require a valid connection to exist before it can be run. You can test this by opening the Data Builder without a database and typing in a create database command in the query window. The database will be created without having a valid connection.
You still have to create the VistaDBConnection object, but you leave the database field blank.
This will create a new database in your current path from SQL Code. This can be very useful when an application is first installed to build the database from scratch in code rather than relying on an installed starter database.
Create a Database in SQL Example |
Copy Code
|
---|---|
using( VistaDB.Provider.VistaDBConnection connection = new VistaDB.Provider.VistaDBConnection() ) { using( VistaDB.Provider.VistaDBCommand command = new VistaDB.Provider.VistaDBCommand() ) { command.Connection = connection; command.CommandText = "CREATE DATABASE 'NEWDB.vdb6', PAGE SIZE 4, LCID 1033, CASE SENSITIVE FALSE;"; command.ExecuteNonQuery(); } } |
The extension .vdb6 is automatically added to the database name by default if no extension is included. If a custom extension is included it will be used (if your edition supports custom extensions).
Executing: