[Visual Basic 6.0]
On Error GoTo MyError
' Set up to read the input data file. Where the file
' format is a comma delimited list of point coordinates.
Dim FileSystem, FileHandle, retstring, position
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set FileHandle = FileSystem.OpenTextFile("D:\NGTest-VB\LoadPoints\pointsource.txt", 1, False)
' Connect to SDE
Dim pPropset As IPropertySet
Set pPropset = New PropertySet
Dim pFact As IWorkspaceFactory
Dim pWorkspace As IWorkspace
With pPropset
.SetProperty "SERVER", "yourserver"
.SetProperty "INSTANCE", "5253"
.SetProperty "DATABASE", "sdedata"
.SetProperty "USER", "test"
.SetProperty "PASSWORD", "test"
.SetProperty "VERSION", "SDE.DEFAULT"
End With
' Open existing target featureclass
Set pFact = New SdeWorkspaceFactory
Set pWorkspace = pFact.Open(pPropset, Me.hWnd)
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspace
' Dim pFeatureDataset As IFeatureDataset
' Set pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("Calibrate")
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("defectos2")
Dim pInsertCursor As IFeatureCursor
Set pInsertCursor = pFeatureClass.Insert(True)
Dim pInsertFeatureBuffer As IFeatureBuffer
Set pInsertFeatureBuffer = pFeatureClass.CreateFeatureBuffer
' Set schema lock and switch to Load Only Mode
Dim pSchLock As ISchemaLock
Set pSchLock = pFeatureClass
pSchLock.ChangeSchemaLock (esriExclusiveSchemaLock)
Dim pFCLoad As IFeatureClassLoad
Set pFCLoad = pFeatureClass
pFCLoad.LoadOnlyMode = True
' Load new point features
Dim pPoint As IPoint
Set pPoint = New Point
Do While FileHandle.AtEndOfLine <> True
retstring = FileHandle.ReadLine
position = InStr(retstring, ",")
pPoint.PutCoords Trim(Left$(retstring, position - 1)), Trim(Right$(retstring, Len(retstring) - position))
Set pInsertFeatureBuffer.Shape = pPoint
pInsertCursor.InsertFeature pInsertFeatureBuffer
Loop
pInsertCursor.Flush
' Clean up
pSchLock.ChangeSchemaLock (esriSharedchemaLock)
pFCLoad.LoadOnlyMode = False
FileHandle.Close
' Message finished
MsgBox "Load Finished"
MyError:
If Err.Number <> 0 Then
MsgBox Err.Description
pSchLock.ChangeSchemaLock (esriSharedchemaLock)
pFCLoad.LoadOnlyMode = False
End If
[C#]//IFeatureClassLoad Example
//IWorkspace is expected to be either an ArcSDE or File Geodatabase workspace.
// e.g., pathOfTextFile = "C:\\data\\pointsource.txt"
// nameOfTargetFeatureClass = "myPointFC"
//on ArcSDE use ISqlSyntax::QualifyTableName for fully qualified table names.
public void IFeatureClassLoad_Example(IWorkspace workspace, string nameOfTargetFeatureClass, string pathOfTextFile)
{
//open the feature class
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(nameOfTargetFeatureClass);
ISchemaLock schemaLock = (ISchemaLock)featureClass;
IFeatureClassLoad featureClassLoad = (IFeatureClassLoad)featureClass;
try
{
//create an insert cursor
IFeatureCursor featureCursor = featureClass.Insert(true);
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
//set schema lock and switch to Load Only ModeschemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
featureClassLoad.LoadOnlyMode = true;
//Load new point features
ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();//setup to read the input data file. where the file format
//is a comma delimited list of point coordinates.
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (System.IO.StreamReader sr = new System.IO.StreamReader(pathOfTextFile))
{
String line; //used to hold the line of text from file
int position; //used to determine the position of the comma
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
position = line.IndexOf(",");
point.PutCoords(Convert.ToDouble(line.Substring(0, position)), Convert.ToDouble(line.Substring(position + 1)));
featureBuffer.Shape = point;
featureCursor.InsertFeature(featureBuffer);
}
}
featureCursor.Flush();//clean up
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
featureClassLoad.LoadOnlyMode = false;Console.WriteLine("Load finished");
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
}
catch (Exception e)
{
//error occured
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
featureClassLoad.LoadOnlyMode = false;
Console.WriteLine(e.Message);
}
}
[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button
in the upper-left corner of the page.