SDE C API Querytable

vb

public function querytable(byval sTable as string,Optional ByVal sWhere As String = "") as long
    Dim sNames() As String
    Dim rv As Long
    Dim lNumTables As Long
    Dim t1() As Byte
    Dim pTableNames() As Long
    Dim pFetchColNames() As Long
    Dim yFetchColNames() As Byte
    Dim hQueryStream As Long
    Dim hQueryInfo As Long
    ' create the query info object
    rv = SE_queryinfo_create(hQueryInfo)
    If rv <> SE_SUCCESS Then
        GoTo ExitClean
    End If
   
    ' define table names for the query
    lNumTables = 1
    ReDim sNames(0 To lNumTables - 1) As String
    sNames(0) = sTable
    If Not StringArray(sNames, t1, pTableNames) Then
        GoTo ExitClean
    End If
   
    ' specify the table to query
    rv = SE_queryinfo_set_tables(hQueryInfo, lNumTables, pTableNames(0), 0)
    If rv <> SE_SUCCESS Then
        GoTo ExitClean
    End If
   
    ' add the shape and unique id columns to the query (or whatever columns you need)
    lNumCols = 2
    ReDim sNames(0 To lNumCols - 1) As String
    sNames(0) = sTable & ".shape"
    sNames(1) = sTable & ".objectid"
    If Not StringArray(sNames, yFetchColNames, pFetchColNames) Then
        GoTo ExitClean
    End If
   
    ' specify the column names for the query
    rv = SE_queryinfo_set_columns(hQueryInfo, lNumCols, pFetchColNames(0))
    If rv <> SE_SUCCESS Then
        GoTo ExitClean
    End If
    ' apply the where clause if one is passed in
    ' NOTE: the where clause is assumed to be valid
    If Len(sWhere) > 0 Then
        rv = SE_queryinfo_set_where_clause(hQueryInfo, sWhere)
        If rv <> SE_SUCCESS Then
            GoTo ExitClean
        End If
    End If
    ' make the stream a query stream
    rv = SE_stream_query_with_info(hQueryStream, hQueryInfo)
    If rv <> SE_SUCCESS Then
        GoTo ExitClean
    End If
   
    ' execute the query
    rv = SE_stream_execute(hQueryStream)
    If rv <> SE_SUCCESS Then
        GoTo ExitClean
    End If
   
    ' iterate over the query records
    rv = SE_stream_fetch(hQueryStream)
    Do While rv = SE_SUCCESS
' do something with the result record
        ' move to the next record
        rv = SE_stream_fetch(hQueryStream)
    Loop
:ExitClean
    ' clean up and free SDE objects here
end function

Private Function StringArray(ByRef sStrings() As String, _
                             ByRef yStrings() As Byte, _
                             ByRef lPointers() As Long) As Boolean
    Const sProcName As String = "StringArray"
    ' helper function to return a C style array of null terminated strings
    ' and pointers to each of the strings from a VB array of variable
    ' length strings (BSTRs), which are not null terminated
    '
    ' NOTE: all arrays are 0 based
    ' NOTE: the calling function must declare the yStrings byte array, and
    ' the lPointers long array so they remain in memory (so the pointers are valid)
    '
    ' the C style string arrays are used in many of the SDE functions that require
    ' pointers to string arrays of table names and column names
    '
    Dim lUb As Long
    Dim lTmp As Long
    Dim lIndex As Long
   
    StringArray = False
   
    On Error GoTo StringArrayErr
   
    lUb = UBound(sStrings)
    ReDim lPointers(0 To lUb)
    ' calculate the total length of the C string array including the null
    ' terminating charcters
    lTmp = 0
    For lIndex = 0 To lUb
        lTmp = lTmp + Len(sStrings(lIndex)) + 1
    Next lIndex
   
    ' resize the byte array to hold the array of C style null terminated strings
    ReDim yStrings(0 To lTmp - 1)
   
    ' copy the vb strings into the byte array
    lTmp = 0
    For lIndex = 0 To lUb
        strcpy yStrings(), sStrings(lIndex) & vbNullChar, lTmp
        lPointers(lIndex) = VarPtr(yStrings(lTmp))
        lTmp = lTmp + Len(sStrings(lIndex)) + 1
    Next lIndex
    'Debug.Print "|" & StrConv(yStrings(), vbUnicode) & "| - " & sModuleName & "." & sProcName
   
    StringArray = True
ExitClean:
    On Error Resume Next
   
    Exit Function
   
StringArrayErr:
    Select Case Err.Number
        Case Else
            'Write2Log Err.Number & " " & Err.Description & " - " & sModuleName & "." & sProcName
            Resume ExitClean
    End Select
   
End Function
Public Function strcpy(ByRef yToCharArray() As Byte, _
                       ByVal sFromString As String, _
                       Optional ByVal lStartPos As Long = 0)
    ' function to copy a vb string into a byte array (C style string)
    Const sProcName As String = "strcpy"
    Static lIndex As Long
    Static lUb As Long
    Static lLb As Long
    Static lCharIndex As Long
   
   
    On Error GoTo strcpyerr
            
    lLb = LBound(yToCharArray) + lStartPos
    lUb = Len(sFromString) - 1 + lLb
    If lUb > UBound(yToCharArray) Then
        lUb = UBound(yToCharArray)
    End If
    lCharIndex = 1
    For lIndex = lLb To lUb
        'Debug.Print Mid$(sFromString, lCharIndex, 1) & " - " & sProcName
        yToCharArray(lIndex) = Asc(Mid$(sFromString, lCharIndex, 1))
        lCharIndex = lCharIndex + 1
    Next lIndex
   
    ' make sure the last character in the string is a null character
    yToCharArray(lUb) = 0
   
    Exit Function
   
strcpyerr:
    'Write2Log Err.Number & " " & Err.Description & " - " & sModuleName & "." & sProcName
End Function


-----------------------------------------------------------------------------------------------------------------------------------------------------------------
C# 

            int rv;
            IntPtr hQueryInfo;
            int lNumTables;
            int lNumCols;
            string[] pTableNames;
            string[] pFetchColNames;
            string sTable = "TEST.BOUND_POINT_1";
            string sWhere = "OBJECTID >0";
            IntPtr hQueryStream;

            //建立连接
            Connection.Se_Error pError = new Connection.Se_Error();
            IntPtr pSdeConn;
            rv = Connection.SE_connection_create(m_strServer, m_strInstance, "SDE", m_strUser, m_strPwd, ref pError, out pSdeConn);
            if (rv != 0) return;
            //建立streame
            rv = Stream.SE_stream_create(pSdeConn, out hQueryStream);
            if (rv != 0) return;
            // create the query info object
            rv = Stream.SE_queryinfo_create(out hQueryInfo);
            if(rv!=0) return;
            // specify the table to query
            lNumTables = 1;
            pTableNames = new string[lNumTables];
            pTableNames[0] =sTable ;
            
            rv = Stream.SE_queryinfo_set_tables(hQueryInfo, lNumTables, pTableNames, null);
           
            //add the shape and unique id columns to the query (or whatever columns you need)
            lNumCols = 2;
            pFetchColNames=new string[lNumCols];
            //pFetchColNames[0] = sTable + ".ID";
            //pFetchColNames[1] = sTable + ".name";
            pFetchColNames[0] = "ID";
            pFetchColNames[1] = "name";
            //specify the column names for the query
            rv = Stream.SE_queryinfo_set_columns(hQueryInfo, lNumCols, pFetchColNames);
            if (rv != 0) return;
            // apply the where clause if one is passed in
            rv =Stream.SE_queryinfo_set_where_clause(hQueryInfo, sWhere);
            if (rv != 0) return;
            
            // make the stream a query stream
            rv =Stream. SE_stream_query_with_info(hQueryStream, hQueryInfo);
            if (rv != 0) return;
            // execute the query
            rv =Stream. SE_stream_execute(hQueryStream);
            if (rv != 0) return;
               
            //iterate over the query records
            rv = Stream.SE_stream_fetch(hQueryStream);
            while(rv==0)
            {   
                // do something with the result record
                long L_INT_VAL;
                rv = Stream.SE_stream_get_integer(hQueryStream, 1, out L_INT_VAL);
                txtMsg.Text += L_INT_VAL.ToString() + "--";
                Byte[] encodedBytes=new byte[50];
                rv = Stream.SE_stream_get_nstring(hQueryStream, 2, encodedBytes);
               
                UnicodeEncoding unicode = new UnicodeEncoding();
                txtMsg.Text += unicode.GetString(encodedBytes) + "*******";
                //move to the next record
                rv = Stream.SE_stream_fetch(hQueryStream);
            }
posted @ 2008-06-23 17:34  明琎  阅读(628)  评论(0)    收藏  举报