using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using SOASystem.Data;
using SOASystem.Data.FileGDBClient;
using SOASystem.Data.GeoDataClient;
using SOASystem.Data.GeoDataTypes;
using Proxies;

namespace FileGDBProviderTest
{
class TableSchema
{
public static bool TestTableSchema()
{
string filePath = @"C:\FileGDBProvider\data\TableSchema\TableSchemaDemo.gdb";
FileGDBConnection conn = new FileGDBConnection("Database=" + filePath);
try
{
conn.Open();
// Delete it if exists
FileGDBConnection.DeleteGeodatabase(filePath);
conn = FileGDBConnection.CreateGeodatabase(filePath);
}
catch (FileGDBException e)
{
// Handle errors
if (e.ErrorCode == -2147024893)
{
Console.WriteLine("FileGDB does not exist");
conn = FileGDBConnection.CreateGeodatabase(filePath);
}
else
Console.WriteLine(e.Message);
}

// Load a feature class XML definition into a string. An example of a data
// element is provided in the sample as "Streets.xml".
//
FileStream fs = new FileStream(@"C:\FileGDBProvider\data\TableSchema\Streets.xml", FileMode.Open);
StreamReader tr = new StreamReader(fs);
string featureClassDef = tr.ReadToEnd();
tr.Close(); fs.Close();

try
{
conn.CreateTable(featureClassDef, "");
Console.WriteLine("The streets table has been created.");
Field fld = CreateStreetsTableTypeField();

FileGDBCommand cmd = new FileGDBCommand("Select * from StateBoundaries", conn);
FileGDBDataReader reader = cmd.ExecuteReader() as FileGDBDataReader;

reader.AddField(fld);

// Alter the field
//
fld.AliasName = "StreetType";
reader.AlterField(fld);
reader.DeleteField("SpeedLimit");

// Add Index
Index index = CreateStreetsTableTypeIndex();
reader.AddIndex(index);

// Add subType
//
Subtype subtype = CreateSubtypeDefinition();
reader.EnableSubtypes("StreetType", subtype);
//reader.CreateSubtype(subtype);
}
catch (FileGDBException ex)
{

}

return true;
}

public static Field CreateStreetsTableTypeField()
{
Field fld = new Field
{
Name = "StreetType",
Type = esriFieldType.esriFieldTypeSmallInteger,
IsNullable = true,
Length = 2,
Precision = 0,
Scale = 0,
AliasName = "Day",
ModelName = "night",
DefaultValue = 1
};
return fld;
}

public static Index CreateStreetsTableTypeIndex()
{
Index index = new Index
{
Name = "StreetTypeIdx",
IsUnique = false,
IsAscending = true,
};

Fields flds = new Fields();
flds.FieldArray = new Field1;
Field fld = new Field
{
Name = "StreetType",
Type = esriFieldType.esriFieldTypeSmallInteger,
IsNullable = false,
Length = 2,
Precision = 0,
Scale = 0,
Required = true,
Editable = false,
DomainFixed = true,
AliasName = "StreetType",
ModelName = "StreetType"
};
flds.FieldArray0 = fld;
index.Fields = flds;
return index;
}

static Subtype CreateSubtypeDefinition()
{
// Create a string containing an XML subtype definition.
Subtype subtype = new Subtype()
{
SubtypeName = "Trunk Highway",
SubtypeCode = 99,
};

subtype.FieldInfos = new SubtypeFieldInfo1;
SubtypeFieldInfo subFieldInfo = new SubtypeFieldInfo()
{
FieldName = "LaneCount",
DomainName = "",
DefaultValue = 10,
};
subtype.FieldInfos0 = subFieldInfo;

return subtype;
}
}
}

Last edited Aug 25, 2011 at 5:20 PM by dotnetprovider, version 1

Comments

No comments yet.