GenerativeComponents Help

Database Code Examples

Here are some script transactions (each taken from a different transaction file) that demonstrate the use of the GCScript database classes.

transaction script 'Create a set of top-level points from a database'
{
 
    // The database already exists, as a SQL Server Express file on our local
    // hard drive. The database contains a table named Table1, which has three
    // columns named X, Y and Z.
 
    DataConnection connection =
                    new DataConnection(DatabaseType.SqlServerLocalDB,
                                        'C:\Sample Databases\Sql Server.mdf');
 
    DataTable tbl =
               new DataTable(connection, 'SELECT * FROM Table1 WHERE X > 1.5');
 
    foreach (DataRow row in tbl)
    {
 
        Point pt = new Point('point' + row.RowIndex);  // Use the row index to
                                                       // give a unique name to
                                                       // each point.
 
        pt.ByCartesianCoordinates(baseCS, row.X, row.Y, row.Z);
 
    }
 
    connection.Close();  // We should always do this when we’re finished.
 
}
transaction script 'Write the child values of point01 to an external file'
{
 
    // Assume that we already have a node named point01, which has been
    // replicated (i.e., it contains a bunch of child points).
    //
    // We will write the data to a text file, in the format of an ADO.NET’s
    // DataSet XML file. In this case, we will not need a DataConnection
    // object.
 
    DataTable tbl = new DataTable
    (
        DataTableType.XmlWithSchema,                       // Table type
        'C:\Sample Database\Output.xml',                   // File path
        {typeof(double), typeof(double), typeof(double)},  // Column types
        {'X',            'Y',            'Z'}              // Column names
    );
 
    tbl.DeleteAllData();   // In case there’s a previous version of the
                           // output file. We don’t want to add to that
                           // existing data; we want to start fresh.
 
    foreach (Point pt in point01)  // Iterate through point01’s child points.
    {
 
        DataRow row = tbl.NewRow();
 
        row.X = pt.X;
        row.Y = pt.Y;
        row.Z = pt.Z;
 
        // Alternately, we could replace the preceding three statements with:
        //
        // row.SetValues(pt.X, pt.Y, pt.Z);
 
    }
 
    tbl.WriteChanges();  // Write all of the table data to the output file,
                         // overwriting the previous version of that file.
 
}