//
// Sample: Querying
//
// Demonstrates how to perform simple spatial and attribute queries.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SOASystem.Data.FileGDBClient;
using SOASystem.Data.GeoDataTypes;
using SOASystem.Data.GeoDataClient;

namespace FileGDBProviderTest
{
class Querying
{
public static bool TestAttributeQuerying()
{
FileGDBConnection conn = new FileGDBConnection(@"database=C:\FileGDBProvider\data\Querying.gdb");
try
{
conn.Open();
}
catch (FileGDBException e)
{
foreach (FileGDBError err in e.Errors)
Console.WriteLine(err.Message + err.ErrorCode);
}

// Perform a simple attribute query: find the names of every city with a 'TERM'
// Value equal to 'City'. Return the NAME, Pop1996 and the X, Y coordinates of
// each feature.
//
String query = "Select Shape, NAME, Pop1996 from Cities WHERE TERM = 'City';";
FileGDBCommand cmd = new FileGDBCommand(query, conn);
FileGDBDataReader r = null;
try
{
r = cmd.ExecuteReader() as FileGDBDataReader;
}
catch (FileGDBException e)
{
foreach (FileGDBError err in e.Errors)
Console.WriteLine(err.Message + err.ErrorCode);
}

if (r == null)
{
conn.Close();
return false;
}

String cityName;
int cityPop;
Point pt;

while (r.Read())
{
cityName = r["Name"].ToString();
cityPop = (int)r["Pop1996"];
pt = (Point)r.GetShape();

Console.WriteLine("City name is {0} population is {1} and Location is {2}", cityName, cityPop, pt);
}

// Close the reader and the filegdb connection
//
r.Close();
conn.Close();
return true;
}

public static bool TestSpatialQuerying()
{
FileGDBConnection conn = new FileGDBConnection(@"database=C:\FileGDBProvider\data\Querying.gdb");
try
{
conn.Open();
}
catch (FileGDBException e)
{
foreach (FileGDBError err in e.Errors)
Console.WriteLine(err.Message + err.ErrorCode);
}

Envelope env = new Envelope
{
XMin = -66.0,
YMin = 46.0,
XMax = -64.0,
YMax = 48.0
};

GeoDataSpatialParameter p = new GeoDataSpatialParameter
{
GeoDataType = SOASystem.Data.GeoDataType.Geometry,
SourceColumn = "Shape",
SpatialRelType = SOASystem.Data.SpatialRelType.SpatialRelIntersects,
Value = env
};
String query = "Select Name from Cities;";

FileGDBCommand cmd = new FileGDBCommand(query, conn);
cmd.Parameters.Add(p);

FileGDBDataReader r = null;
try
{
r = cmd.ExecuteReader() as FileGDBDataReader;
}
catch (FileGDBException e)
{
foreach (FileGDBError err in e.Errors)
Console.WriteLine(err.Message + err.ErrorCode);
}
if (r == null)
{
conn.Close();
return false;
}

String cityName;

while (r.Read())
cityName = r"Name".ToString();

// Close the reader and the filegdb connection
//
r.Close();
conn.Close();

return true;
}
}
}

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

Comments

No comments yet.