Select to view content in your preferred language

(VB.NEt) oledb connection to a shapefile in a geodatabase

841
1
03-10-2011 03:36 AM
SébastienGagnon
Emerging Contributor
I would like to make SQL queries from  a table in a shapefile store in a personal geodatabase. For a simple shapefile i use this VB code:

Public Sub DisplayNamesFromShapefile()

        ' Build the connection string.
        Dim shapefileStringTemplate As String = "Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" & "Extended Properties=workspacetype=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry={1}"
        Dim connectionString As String = String.Format(shapefileStringTemplate, "C:\temp\", "WKB")

        ' Create the connection.
        Dim oleDbConnection As OleDbConnection = New OleDbConnection()
        oleDbConnection.ConnectionString = connectionString

        ' Open the connection and create a reader.
        oleDbConnection.Open()


        ' Create the command.
        Dim sqlQuery As String = "SELECT * FROM Province"
        Dim oleDbCommand As OleDbCommand = New OleDbCommand(sqlQuery, OleDbConnection)


        Dim oleDbDataReader As OleDbDataReader = OleDbCommand.ExecuteReader()

        ' Display the values from the NAME field.
        Do While oleDbDataReader.Read()
            Console.WriteLine(oleDbDataReader("Name").ToString())
            Console.WriteLine(oleDbDataReader("Code").ToString())
            Console.WriteLine(oleDbDataReader("Area").ToString())
            Console.WriteLine(oleDbDataReader("Pop1991").ToString())
        Loop

    End Sub

But if my shapefile is store in a personal geodatabes how to proceed?

thanks,
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
Hi,

A "shapefile" in a Personal GeoDatabase is not called a Shapefile it is a "FeatureClass".

You would normally use ArcObjects to access a FeatureClass in a Personal GeoDatabase and there are many examples in Help and on the Archived forum.

But you want to access the Personal GeoDatabase using OLE, I'm guessing you are developing on a machine that does not have ArcGIS installed on it? If so then the example code below will help you.

' connect to personal geodatabase called C:\Temp\pGDB_Scratch.mdb
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\pGDB_Scratch.mdb"

' Create the connection.
Dim oleDbConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection()
oleDbConnection.ConnectionString = connectionString

' Open the connection and create a reader.
oleDbConnection.Open()

' Create the command to select all fields from a FeatureClass called CompassPoints
Dim sqlQuery As String = "SELECT * FROM CompassPoints"
Dim oleDbCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlQuery, oleDbConnection)
Dim oleDbDataReader As OleDb.OleDbDataReader = oleDbCommand.ExecuteReader()

' Display data in debug window
Do While oleDbDataReader.Read()
    Debug.Print(oleDbDataReader("ID").ToString())
    Debug.Print(oleDbDataReader("Angle").ToString())
Loop


Duncan
0 Kudos