VistaDB 6
VistaDB / Developer's Guide / How To Perform Common Tasks / Create a New Database in Code / How To Create a new database using DDA
In This Topic
    How To Create a new database using DDA
    In This Topic

    Creating a database using the DDA (Direct Data Access) objects is a very simple process.

    The first thing you must do is obtain a valid connection to the DDA Engine of VistaDB. Then you call the CreateDatabase() function with the parameters completed. This example creates a database, builds a test table with some column types, and then inserts some data into the tables.

    After building the database you can then open it in the Data Builder and view the results (make sure your app and Data Builder are not in exclusive mode or you will not be able to open it).

    Example

    DDA Create Database Example
    Copy Code
    // We need a connection to the DDA engine
    IVistaDBDDA DDAObj = VistaDBEngine.Connections.OpenDDA();
    
    // Create a NEW database
    IVistaDBDatabase mynewDatabase = DDAObj.CreateDatabase(filename, true, null, 2, 0, false);
    
    IVistaDBTableSchema table1s = mynewDatabase.NewTable("TestTable1");
    Assert.IsNotNull(table1s, "TestTable1 creation failed");
    table1s.AddColumn("ID", VistaDBType.Int);
    table1s.DefineColumnAttributes("ID", false, false, false, false, null, null);
    
    table1s.AddColumn("COLINT", VistaDBType.Int);
    table1s.DefineColumnAttributes("COLINT", false, false, false, false, null, null);
    
    table1s.AddColumn("COLDATETIME", VistaDBType.DateTime);
    table1s.DefineColumnAttributes("COLDATETIME", false, false, false, false, null, null);
    
    table1s.AddColumn("COLCHAR128", VistaDBType.Char, 128);
    table1s.DefineColumnAttributes("COLCHAR128", false, false, false, false, null, null);
    
    // Now that we have the table schema defined, create the table
    IVistaDBTable newTable = mynewDatabase.CreateTable(table1s, false, false);
    
    // Create some indexes
    newTable.CreateIdentity("ID", "1000", "1");
    newTable.CreateIndex("Primary", "ID", true, true);
    newTable.CreateIndex("idxDate", "COLDATETIME", false, false);
    
    // Generate some data quickly for the table
    for( int i = 0; i < 1000; i++ )
    {
       newTable.Insert();
       newTable.PutInt32("COLINT", i);
       newTable.PutDateTime("COLDATETIME", DateTime.Now);
       newTable.PutString("COLCHAR128", "SomeChar" + i.ToString());
       newTable.Post();
    }
    
    newTable.Close();
    newTable.Dispose();
    newTable = null;
    
    mynewDatabase.Close();
    mynewDatacase = null; 
    
     
    


      Last Updated: 08/21/09 | © Infinite Codex, Inc., 2010

    See Also