Examples on how to use the C API in .NET

esri 上面的。  


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SDEConn
{
// Structlayout
// Declares a managed structure for each unmanaged structure.
// SE_ERROR
[ StructLayout( LayoutKind.Sequential  )]
public struct Se_Error
{
  public System.Int32 sde_error;
  public System.Int32 ext_error;
  [ MarshalAs( UnmanagedType.ByValArray, SizeConst=512 )]
  public char[] err_msg1;
  [ MarshalAs( UnmanagedType.ByValArray, SizeConst=4098 )]
  public char[] err_msg2;
}
// SE_CONNECTION
[ StructLayout(  LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct Se_Connection
{
  public System.IntPtr hndl;
};
// SE_STREAM
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct Se_Stream
{
  public System.IntPtr hndl;
};

public class
  LibWrap   
{
  // Declares a managed prototype for unmanaged function.
  // SE_connection_create
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_connection_create(string server,
   string instance,
   string database,
   string username,
   string password,
   ref Se_Error error,
   ref Se_Connection conn);
  // SE_connection_free
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern void SE_connection_free(Se_Connection conn);   
  // SE_Stream_Create
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32  SE_stream_create (Se_Connection conn, ref Se_Stream str);
  // SE_Stream_Free
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32  SE_stream_free (Se_Stream str);
  // SE_Stream_Prepare_SQL
  [DllImport("sde90.dll",
   SetLastError = true,
   ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_prepare_sql(Se_Stream str, string sql_stmt);
  
  // Delete Records from Table
  [DllImport("sde90.dll",
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_delete_from_table (Se_Stream stream, string table, string where_clause);
  // Cantidad de Columnas
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_num_result_columns (Se_Stream str, ref int num_columns);
  // Error
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_get_ext_error (Se_Stream str, ref Se_Error error);
  // Stream Execute
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_execute (Se_Stream str);

  // Insert Table
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_insert_table(Se_Stream str, string table, int num_columns, string[] columns);
  
  // Insert String
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_set_string (Se_Stream str, int column, string Value);
  // Insert Long
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32  SE_stream_set_integer (Se_Stream str, int column,  ref long Value);
  // Update
  [DllImport("sde90.dll",  
    SetLastError = true,
    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_update_table(Se_Stream str, string Table, int num_columns, string[] columns, string where_clause);

}

/// <summary>
/// Summary description for SDEConnect.
/// </summary>
public class SDEConnect
{
  public static void Main()   
  {
   Se_Connection pConn;
   Se_Error pError;
   long rc;
         
   pConn = new Se_Connection();
   pError = new Se_Error();
   
   // El Stream
   Se_Stream pStr;
   pStr = new Se_Stream();
   // connect to SDE
   try
   { Console.WriteLine("connecting");
    rc = LibWrap.SE_connection_create("erks", "5151", "", "sde", "sde", ref pError, ref pConn);
   
    // Creo el STREAM
    rc = LibWrap.SE_stream_create (pConn,ref pStr);
    // Delete
    // ------
     
    rc = LibWrap.SE_stream_create (pConn,ref pStr);
    rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
    rc = LibWrap.SE_stream_delete_from_table (pStr,"PruebaObjectID","OBJECTID >= 7 and OBJECTID <= 10");
    rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
   

    // Insert
    // ------
    /*
     string[] Cols;
        Cols = new string[3];
     Cols[0] = "VALOR";
     Cols[1] = "NOMBRE";
     Cols[2] = "EDAD";
   
     rc = LibWrap.SE_stream_insert_table ( pStr, "PruebaOBJECTID", 3, Cols);
     // Inserto
     long Valor = 900;
     rc = LibWrap.SE_stream_set_integer(pStr, 1 , ref Valor);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     
     string ValorSTR = "Trulala";
     rc = LibWrap.SE_stream_set_string (pStr,2, ValorSTR);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     
     Valor = 5000;
     rc = LibWrap.SE_stream_set_integer (pStr,3,ref Valor);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     // Execute
     rc = LibWrap.SE_stream_execute ( pStr);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
    */
     
    // Update
    // ------
    /*
     string[] Cols;
     Cols = new string[2];
     Cols[0] = "VALOR";
     Cols[1] = "EDAD";
     
     string Where_Clause = "NOMBRE = 'FEDERICO'";
     rc = LibWrap.SE_stream_update_table (pStr, "PruebaOBJECTID", 2, Cols, Where_Clause);
     long Valor = 102;
     rc = LibWrap.SE_stream_set_integer (pStr,1,ref Valor);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     Valor = 103;
     rc = LibWrap.SE_stream_set_integer (pStr,2,ref Valor);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     // Execute
     rc = LibWrap.SE_stream_execute ( pStr);
     rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
     
    */
     
    // Libero el STREAM
    rc = LibWrap.SE_stream_free (pStr);


   }
   catch(Exception  ex)
   {
    Console.WriteLine(ex.Message + "this is err");
   }
   // disconnect from SDE
   LibWrap.SE_connection_free(pConn);
  }

}
}
posted @ 2008-06-23 17:35  明琎  阅读(730)  评论(8)    收藏  举报