ArcSDE c api 在C#中开发记录

来源:互联网 发布:最新传奇扫号软件 编辑:程序博客网 时间:2024/05/16 17:08
用c#中相应的类型把指针类型替换掉就可以了
  如:
1)、char*,可以用string来代替  要返回的话需要用StringBuilder 因为string只可以传递,不能返回 传值,而不是传引用(ref也不行, ref只适用于传递struct)
2)、char**,(二级指针)可以用string[]来代替,或者用arraylist来代替可以做为函数参数以ref传递,但  如果是用在struct中的字段,则这样就不行了 如下:

LONG SE_stream_fetch_row
(SE_STREAM stream,
const CHAR *table,
LONG sde_row_id,
SHORT num_columns,
const CHAR **columns);

这里可以用char**就是字符串的指针,这里,你可以使用StringBuilder[]来替代。

publicstruct Se_Sql_Construct
{
publicInt32 num_tables;
[MarshalAs(UnmanagedType.ByValArray)]
publicStringBuilder[] tables;
publicstring where;
}
3)、CHAR err_msg1[SE_MAX_MESSAGE_LENGTH];     用[ MarshalAs( UnmanagedType.ByValArray, SizeConst=512 )]
  public char[] err_msg1; 来代替
 
4)、LONG *int_val  用ref long Value 声明  否则出错“不能访问内存”
5)、struct *dd 用ref struct
6)、关键字对应
in ==> ref
out ==> out
ref(C# 参考)ref 关键字使参数按引用传递。 其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。
class
 RefExample2
  {
static void Method(ref string s)
      {          
s = "changed";
      }
 
static void Main()
      {
string str = "original";
          Method(ref str);
          Console.WriteLine(str);
      }
  }
 
// Output: changed
 
out 参数修饰符(C# 参考) 

out 关键字会导致参数通过引用来传递。 这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。 若要使用out 参数,方法定义和调用方法都必须显式使用 out 关键字。 例如:

 
1、SE_stream_get_integer 
 
C中定义:LONG SE_stream_get_integer(SE_STREAM stream,SHORT column,LONG *int_val);
C#中声明:
 
[DllImport("sde90.dll",      SetLastError = true,    ThrowOnUnmappableChar = true)]
 
public static extern System.Int32  SE_stream_set_integer (Se_Stream str, int column,  ref long Value);
 
2、SE_stream_set_string
C中定义:LONG SE_stream_set_string(SE_STREAM stream,SHORT column,const CHAR *string_val);
 
C#中声明:
 
[DllImport("sde90.dll",     SetLastError = true,    ThrowOnUnmappableChar = true)]
public static extern System.Int32 SE_stream_set_string (Se_Stream str, int column, string Value);
 
3、SE_SQL_CONSTRUCT
 
C中定义:
/************************************************************
*** SDE SQL CONSTRUCTOR
************************************************************/
typedef struct {
    LONG       num_tables;    /* number of tables */
    CHAR       **tables;      /* table names */
    CHAR       *where;        /* where clause */
} SE_SQL_CONSTRUCT;
C#中声明:
 
// Struct layout:
[StructLayout(LayoutKind.Sequential)]
public struct Se_Sql_Construct
{
    public Int32 num_tables;
    public IntPtr tables;
    public string where;
}
 
4、SE_stream_query  复杂定义 CHAR **columns
 
C中定义:
LONG SE_stream_query(SE_STREAM stream,SHORT num_columns,const CHAR **columns,const SE_SQL_CONSTRUCT *construct);
 
C#中声明:
 
[DllImport(".\\sde91.dll", SetLastError = true, ThrowOnUnmappableChar = true)]
 
internal static extern Int32 SE_stream_query(IntPtr Stream, Int16 num_columns, [In, Out] String[] arrColumns, [In] IntPtr SqlConstruct);
应用:
 
// allocate Memory in Sde for the Struct to be passed
lRc = SE_sql_construct_alloc(1, out ppSqlConstruct);
// assign the allocated struct to the local
Se_Sql_Construct ptest = (Se_Sql_Construct)Marshal.PtrToStructure(ppSqlConstruct, typeof(Se_Sql_Construct));
// 1 Table to query from
sArrTables = new String[] { "k_a008_pcsxq_pg" };
// Preparing to pass a char**
IntPtr[] intptrArray = new IntPtr[sArrTables.Length];
for (int i = 0; i < sArrTables.Length; i++)
{
intptrArray[i] = Marshal.StringToHGlobalAnsi(sArrTables[i]);
}
//Get the pinned address of the IntPtr array
GCHandle gchandle = GCHandle.Alloc(intptrArray, GCHandleType.Pinned);
IntPtr ppChar = gchandle.AddrOfPinnedObject();
// fill the SQL construct
pSqlConstr.num_tables = 1;
pSqlConstr.tables = ppChar;
pSqlConstr.where = "OBJECTID < 5";
// Allocate memory to Marshal the Struct
ppSqlConstruct = Marshal.AllocHGlobal(Marshal.SizeOf(pSqlConstr));
Marshal.StructureToPtr(pSqlConstr, ppSqlConstruct, false);
sArrAttrs = new String[] { "OBJECTID" };
// ready..
lRc = SE_stream_query(pStream, 1, sArrAttrs, ppSqlConstruct);
 
4、SE_stream_insert_table 简单定义  CHAR **columns  传递参数时,可用用数组代替char**(只能用作功能函数的参数传递,不能用作结构体中)
C中定义:LONG SE_stream_insert_table(SE_STREAM stream,const CHAR *table,SHORT num_columns,const CHAR **columns);
C#中声明:
 
// 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);
 
应用:
 
    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);
 
 
5、SE_stream_get_ext_error 简单定义 Stuct
 

C中定义:

/************************************************************
*** SDE ERROR STRUCTURE
************************************************************/
typedef struct {
    LONG sde_error;                          /* SDE error code */
    LONG ext_error;                          /* DBMS or OS errno code */
    CHAR err_msg1[SE_MAX_MESSAGE_LENGTH];    /* DBMS or SE_error_get_string   msg */
    CHAR err_msg2[SE_MAX_SQL_MESSAGE_LENGTH];/* Second DBMS error, if  applicable */
} SE_ERROR;
LONG SE_stream_get_ext_error(SE_STREAM stream,SE_ERROR *error);
 
C#中声明:
// 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;
}
 [DllImport("sde90.dll",      SetLastError = true,    ThrowOnUnmappableChar = true)]
  public static extern System.Int32 SE_stream_get_ext_error (Se_Stream str, ref Se_Error error);
rc = LibWrap.SE_stream_get_ext_error(pStr,ref pError);
6、SE_error_get_string  CHAR *返回值  如果用string (即使加ref)也不能不能返回值 用stringbuider但不需要加ref 因为它是地址传递

C中定义:

LONG SE_error_get_string (LONG error_code CHAR *error_string);

C#中声明:
publicstatic extern System.Int32 SE_error_get_string(Int32 error_code,StringBuilder error_string);
调用
StringBuilder error_string =new StringBuilder();
ret = SE_error_get_string(rc,error_string);
 
7、定义 SE_layerinfo_get_spatial_column   CHAR *返回值 
 
C定义如下:

LONG SE_layerinfo_get_spatial_column (const SE_LAYERINFO layer, CHAR *table, CHAR *column);

解决方案1:

c#定义如下

[DllImport(".\\sde91.dll", SetLastError = true, ThrowOnUnmappableChar = true)]

publicstatic externInt32 SE_layerinfo_get_spatial_column(IntPtr layerinfo, [MarshalAs(UnmanagedType.LPStr)]out StringBuilder layername, [MarshalAs(UnmanagedType.LPStr)]out StringBuilder column);
 
调用:
StringBuilder layer_name =new StringBuilder(1808);
StringBuilder layer_column =new StringBuilder(256);
 
lRc3 = SE_layerinfo_get_spatial_column(pArrayLayerList[i],out layer_name, out layer_column);
出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误
 
正确方式:
publicstatic externInt32 SE_layerinfo_get_spatial_column(IntPtr layerinfo, [Out,MarshalAs(UnmanagedType.LPArray, SizeConst = 1808)]byte[] layername, [Out,MarshalAs(UnmanagedType.LPArray, SizeConst = 256)]byte[] column);
 
byte[] layer_name =new byte[1808];
byte[] layer_column =new byte[256];
 
lRc3 = SE_layerinfo_get_spatial_column(pArrayLayerList[i], layer_name, layer_column);
 
//用于将byte树组转换为string
public   static   string   FromASCIIByteArray(byte[]   characters)

        ASCIIEncoding   encoding   =   new   ASCIIEncoding(); 
        string   constructedString   =   encoding.GetString(characters); 
        return   (constructedString);
 
 
解决方案2:用stringbuilder不应再加ref
publicstatic externInt32 SE_layerinfo_get_spatial_column(IntPtr layerinfo,StringBuilder layername, StringBuilder column);
StringBuilder layer_name =new StringBuilder();
StringBuilder layer_column =new StringBuilder();
lRc3 = SE_layerinfo_get_spatial_column(pArrayLayerList[i], layer_name, layer_column);
 
8、二级指针参数: **pp  传递事例 (用于返回)
比如,下面的C++代码,在C#中要怎么写 :
void fun1(TStudent **pp)
{
  (*p) = malloc(sizeof(TStudent));
  ...
}
void main()
{
  TStudent *p = NULL;
  fun1(&p);
  free(*p);
  *p = NULL;
}
 
对应C#代码:
[StructLayout(LayoutKind.Sequential)]
publicstruct TStudent
{
    publicint age;
}
 
[DllImport(DllName, CharSet = CharSet.Auto, EntryPoint= "fun1")]
staticextern void fun(refIntptr student);
 
////使用的时候//public
TStudent Get(){
 
int size= Marshal.SizeOf(typeof(TStudent));  
IntPtr ptr = Marshal.AllocHGlobal(size);
try
{
 
   fun(refIntptr ptr);
}
finally
{
  Marshal.FreeHGlobal(ptr);
}
}
 
 
9、C结构体指针之C#结构体的定义:传送
[StructLayout(LayoutKind.Sequential)]  
 
public struct VGAStat  
 
{  
 
public int ChannelNum;//通道数量 
 
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]  
 
public char[] Version;//版本信息 
 
public uint CPUUsage;//CPU占用 
 
public bool WorkStatusOk; //工作状态 
 
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]  
 
public tagCheckArg[] ChannelStatistic;//通道信息 
 

定义完结构体后,就可将接收到的C#结构体指针转换为定义的结构体对象。

  1. VGAStat entries = (VGAStat)Marshal.PtrToStructure(iptr, typeof(VGAStat));  

  2. //iptr为接收到的非托管的结构体指针。 

反之,也可将结构体赋值后封送到非托管内存。

假如vga为定义后实例化并赋值了的结构体。

  1. IntPtr intptr = Marshal.AllocHGlobal(Marshal.SizeOf(vga));  

  2. Marshal.StructureToPtr(vga, intptr, true);  

  3. //在此发送intptr指针给目的方 

  4. Marshal.FreeHGlobal(intptr);//释放分配的非托管内存。 
10、SE_WCHAR *wchar_val 参数传递
 
c函数原型:如下
LONG SE_stream_get_nstring (SE_STREAM stream, SHORT column, SE_WCHAR *wchar_val);
 
以下为错误的方法:
c#
 
publicstatic externint SE_stream_get_nstring(IntPtr stream,int column,refstring wchar_val);
调用:
string encodedBytes ="";
rv = SE_stream_get_nstring(hQueryStream, 2,ref encodedBytes);
出现错语:
引发类型为“System.ExecutionEngineException”的异常。
 
调整为  调用成功,但返回为乱码
publicstatic externint SE_stream_get_nstring(IntPtr stream,int column,StringBuilder wchar_val);
StringBuilder encodedBytes =new StringBuilder();
rv = SE_stream_get_nstring(hQueryStream, 2, encodedBytes);
效果如:
中国电信大水头营业厅
-N榻?u閰?Y4l4Y%?N鍖?
即使用convert转码也只能转为:中国电信大水头㼥蕎�  因为接收到时已经出了"?" ,这时再转就丢失了,因为正确做法如下:
publicstatic externint SE_stream_get_nstring(IntPtr stream,int column, Byte[] wchar_val);
Byte[] encodedBytes =new byte[50];
rv = SE_stream_get_nstring(hQueryStream, 2, encodedBytes);
UnicodeEncoding unicode =new UnicodeEncoding();
string sss = unicode.GetString(encodedBytes) ;
 
11、SE_LAYERINFO layer  和SE_LAYERINFO* layer   函数参数传递 第一个为结构体指针,第二个是二级指针,指向结构体指针的指针
 
指针也只是一个整形数值而已 int 和intptr 一样。
 
SE_LAYERINFO  在c中无定义,就是结构体指针

LONG SE_layerinfo_create (const SE_COORDREF coordref, SE_LAYERINFO *layer);

传过地址,分配空间,并返回  也就是说带* 的结构体,在C#中可不用分配空间,函数会分配,无*则前提必须已分配空间,否则报handle非法的错误(-134 The given shape object handle is invalid.)

LONG SE_layer_get_info (SE_CONNECTION connection, const CHAR *table, const CHAR *column, SE_LAYERINFO layer);

直接修改地址存的内存值,就可以达到修改值的目的

方案一、定义如下 采用int方式

internalstatic externint SE_layerinfo_create(string coordref,ref int layer);
internal staticextern int SE_layer_get_info(IntPtr connection,string table, string column,int layerinfo);
 
方案二、定义如下
定义为指针类型
publicstatic externint SE_layerinfo_create(IntPtr coordref,ref IntPtr layer);  //注意加了ref
publicstatic externint SE_layer_get_info(IntPtr connection,string table, string column,IntPtr layerinfo);  //注意未加ref
 
如果加了ref,则会出-2 错误 The pointer to SE_LAYERINFO is not initialized.
 
下面函数加了ref同样会出现 -134  错误:The given shape object handle is invalid.
rv = SE_stream_get_shape(hQueryStream,3,ref shp);
 
12: 关于函数中结构参数 带* 和不带*的区别
[StructLayout(LayoutKind.Sequential)]
publicstruct SE_Envelope
{
publicdouble minx;
publicdouble miny;
publicdouble maxx;
publicdouble maxy;
}
 

原C定义:LONG SE_coordref_get_xy_envelope (SE_COORDREF coordref, SE_ENVELOPE *extent);

C#定义:
[DllImport("sde91.dll", SetLastError=true)]
internalstatic externint SE_coordref_get_xy_envelope(int coord,out SE_Envelope xyextent);
使用时:
SE_Envelope envelope;
SE_coordref_get_xy_envelope(coordref,out envelope)
 

LONG SE_layerinfo_create (const SE_COORDREF coordref, SE_LAYERINFO *layer);

LONG SE_layer_get_info (SE_CONNECTION connection, const CHAR *table, const CHAR *column, SE_LAYERINFO layer);

publicstatic externint SE_layerinfo_create(IntPtr coordref,ref IntPtr layer);
publicstatic externint SE_layer_get_info(IntPtr connection,string table, string column,IntPtr layerinfo);
 
IntPtr layer =IntPtr.Zero;
rv= SE_layerinfo_create((IntPtr)null,ref layer);
rv= SE_layer_get_info(pSdeConn,"k_j001_yz_pt", "shape", layer) ;
 
 
 
 
13、以下这句很典型,是指针应用的典范,指针就是一个int数值
[StructLayout(LayoutKind.Sequential, CharSet =CharSet.Ansi)]
publicstruct SE_INSTANCE_USER
{
publicInt32 svrpid;
publicInt32 cstime;
publicbool xdr_needed;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
publicchar[] sysname;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
publicchar[] nodename;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
publicchar[] username;
}
publicstatic externInt32 SE_instance_get_users( string server,string instance, outIntPtr user_list, outInt32 user_count );
 
IntPtr userList;
int iRc =Connection.SE_instance_get_users( m_strServer, m_strInstance,out userList, out userCount );
Connection.SE_INSTANCE_USER[] pUsers =new Connection.SE_INSTANCE_USER[userCount];
IntPtr current = userList;
DataRow newRow;
for(int i = 0; i < userCount; i++ )
{
newRow = changeTable.NewRow();
pUsers[i] =new Connection.SE_INSTANCE_USER();
pUsers[i] = (Connection.SE_INSTANCE_USER)Marshal.PtrToStructure( current,typeof(Connection.SE_INSTANCE_USER) );
Marshal.DestroyStructure( current,typeof(Connection.SE_INSTANCE_USER) );
//这里加了指针移动
current = (IntPtr)((int)current +Marshal.SizeOf( pUsers[ i ] ));
}
 
 
14、从结构体向指针赋值,需先给指针分配内存空间
public struct Point
  {
public int x;
public int y;
  }
 Point p;
p.x = 1;
 p.y = 1;
IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));
 
Marshal.StructureToPtr(p, pnt, false);
 
15、应用StringBuilder 出现缓冲区溢出错误:
 
错误如下:
publicstatic externint SE_shape_as_text(IntPtr shape,int alloc_size,StringBuilder text_shape);
应用
StringBuilder pWKT =new StringBuilder();
rv = SE_shape_as_text(shp, size, pWKT);
警告: 非托管代码已经在 StringBuilder 缓冲区溢出。进程可能变得不稳定。在进行封送处理之前,未给 StringBuilder 分配足够的容量。
 
因为stringbuilder长度默认为16位,超过就出来此种错误,(只有在非托管不会自动扩展),因此需要加定义宽度。
改如下:
int size = 0;
SE_shape_get_text_size(shp,ref size);
StringBuilder pWKT =new StringBuilder(size);
rv = SE_shape_as_text(shp, size, pWKT);
 
15、结构体 参数加* 的传递,加ref 如果不加,则出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误
 

原C:

LONG SE_shape_generate_rectangle (SE_ENVELOPE *rect, SE_SHAPE tgt_shape);

C#定义:

[DllImport(".\\sde91.dll", SetLastError = true)]

publicstatic externint SE_shape_generate_rectangle(refSE_ENVELOPE rect, IntPtr tgt_shape);
SE_ENVELOPE clipEnvelope =new SE_ENVELOPE();
clipEnvelope.xMin = 103;
clipEnvelope.yMin = 36;
clipEnvelope.xMax = 105;
clipEnvelope.yMax = 37;
//生成矩形
lRc = SE_shape_generate_rectangle(ref clipEnvelope, ptrShape);
 
 
16、结构体中char[]赋值,关于char[]定义 传递时要用array.copy复制,保持长度一致,否则出现"未能封送类型,因为嵌入数组实例的长度与布局中声明的长度不匹配"错误
c定义如下:

typedef struct {

  CHAR table[SE_QUALIFIED_TABLE_NAME]; /* The spatial table name */

  CHAR column[SE_MAX_COLUMN_LEN];      /* The spatial column name */

  LONG filter_type;                    /* The type of spatial filter */

} SE_FILTER;

c#定义:
publicstruct SE_FILTER
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 226)]
    publicchar[] table; //表名
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    publicchar[] column;//shape字段名 可以用SE_layerinfo_get_spatial_column获取
    publicInt32 filter_type; //#define SE_SHAPE_FILTER 1 #define SE_ID_FILTER 2
}
调用:
char[] table =new char[226];
Array.Copy("K_C001_GAJG_PT".ToCharArray(), table, 14);
pFilter.table = table;
char[] column =new char[32];
Array.Copy("shape".ToCharArray(), column, 5);
pFilter.column = column;
 
17、关于结构体指针 参数传递
c定义如下:

LONG SE_stream_set_spatial_constraints(SE_STREAM stream,SHORT search_order,BOOL calc_masks,SHORT num_filters,constSE_FILTER *filters);

关于se_filters, 说明是个数组

filtersAn array of spatial filters
在C#中可有两种方式调用:
第一种:指针方式
publicstatic externint SE_stream_set_spatial_constraints(IntPtr Stream,Int16 search_order, bool calc_masks,Int16 num_filters, IntPtr filters);
SE_FILTER pFilter =new SE_FILTER();
//结构体转指针类型
IntPtr ptrFilter =Marshal.AllocHGlobal(Marshal.SizeOf(pFilter));
Marshal.StructureToPtr(pFilter, ptrFilter,false);
lRc = SE_stream_set_spatial_constraints(pStream, 1,false, 1, ptrFilter);
第二种:数组方式
publicstatic externint SE_stream_set_spatial_constraints(IntPtr Stream,Int16 search_order, bool calc_masks,Int16 num_filters, SE_FILTER[] filters);
SE_FILTER[] SE_FILTERArray =new SE_FILTER[1];
SE_FILTERArray[0] = pFilter;
lRc = SE_stream_set_spatial_constraints(pStream, 1,false, 1, SE_FILTERArray);
 
第三种:只传结构体,则报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
publicstatic externint SE_stream_set_spatial_constraints(IntPtr Stream,Int16 search_order, bool calc_masks,Int16 num_filters, SE_FILTER filters);
 
18、创建shape时,必须加坐标系,否则生产出的shape不准确,而且有时有错误(小精度时)
//创建一个shape,必须用SE_shape_create
IntPtr ptrShape =IntPtr.Zero;
//为指针分配内存
//获取图层坐标系
IntPtr layer =IntPtr.Zero;
lRc = SE_layerinfo_create((IntPtr)null,ref layer);
lRc = SE_layer_get_info(pConn,"k_j001_yz_pt", "shape", layer);
IntPtr coordref =IntPtr.Zero;
lRc = SE_coordref_create(ref coordref);
lRc = SE_layerinfo_get_coordref(layer, coordref);
//{POLYGON (( 105.00000000 37.00000000, 104.00000000 38.00000000, 103.00000000 37.00000000, 103.00000000 36.00000000, 104.00000000 36.00000000, 105.00000000 36.00000000, 105.00000000 37.00000000))}
//lRc = SE_shape_create(IntPtr.Zero, ref ptrShape);
//如果指定了坐标系,则更精确 必须加坐标系
//105.18200000 36.54400000, 104.88910678 37.25110678, 104.18200000 37.54400000, 103.47489322 37.25110678, 103.18200000 36.54400000, 103.47489322 35.83689322, 104.18200000 35.54400000, 104.88910678 35.83689322, 105.18200000 36.54400000)
lRc = SE_shape_create(coordref,ref ptrShape);
 
18、函数传递中二级指针** 其中SE_COLUMNINFO  是个指针类型  **表示指针数组
c中原型如下:

LONG SE_table_get_column_list (SE_CONNECTION handle, const CHAR *table, SE_COLUMNINFO **column_list, SHORT *column_count);

column_listThe address of the list of column descriptions for the specified registered table
在c#中定义: 加out 或ref
 
publicstatic externint SE_table_get_column_list(IntPtr Connection,string table,outIntPtr column_list,reflong column_count);
long dd = 0;
 
lRc = SE_table_get_column_list(pConn,"K_C001_GAJG_PT",out columns,ref dd);
//定义指针数组
IntPtr[] destination =new IntPtr[dd];
//转到托管内存中
Marshal.Copy(columns, destination, 0,(int) dd);
StringBuilder columnname=newStringBuilder();
lRc = SE_columninfo_get_name(destination[1], columnname);
 
free释放columns
 
19、数据库类型
 
SE_stream_get_integer  读整型, 数据库中是integer 或number
SE_stream_get_string  不能读取 整型
 
报错: -66

SE_INVALID_PARAM_VALUE     (-66)

Returned from a function that was given an invalid parameter value. The function is unable to continue.

lRc = SE_stream_query(pStream, 2, sArrAttrs,ref pSqlConstr);
查询时为1的列,数量列不足
 

SE_WRONG_COLUMN_TYPE     (-114)

Returned when trying to set or get the value of a column in a stream using the wrong column type. For example, calling SE_stream_get_integer on a column that is an SE_FLOAT.

表示数据类型与读取函数不匹配,比如整型读取字符串字段

20、 SE_stream_update_table函数,更新注意,必须查出来几个字段都必须更新,否则报-90错误 (注:如果查出来多条记录,则都更新)

SE_COLUMN_NOT_BOUND     (-90)

A column was specified for an insert/update operation but was not bound (by calling SE_stream_bind_input_column), or the value was not set at the time of execution.

string[] Cols;
Cols = new string[1];
Cols[0] ="DDD";
string Where_Clause ="OBJECTID = 5";
lRc = SE_stream_update_table(pStream,"K_C001_GAJG_PT", 1, Cols, Where_Clause);
//SE_stream_update_row
long Valor = 1;
lRc = SE_stream_set_integer(pStream, 1,out Valor);
check_error(lRc, pStream);
//Execute
lRc = SE_stream_execute(pStream);
 
21、LFLOAT *z 表示数组
c原定义如下:

LONG SE_shape_generate_point (LONG num_pts, SE_POINT *point_array, LFLOAT *z, LFLOAT *measure, SE_SHAPE tgt_shape);

C#错误定义:

publicstatic externint SE_shape_generate_point(int num_pts,SE_POINT[] point_array,double z,double measure, IntPtr shape_val);

报错:

SE_POINT pnt =new SE_POINT();

pnt.x = 28;

pnt.y = -25;

SE_POINT[] pnts = { pnt };

//Here's the problem.

lRc = SE_shape_generate_point(1, pnts, 0, 0, shape);

SE_INVALID_SHAPE_OBJECT     (-134)

The given shape object handle is invalid.

C#正确定义:

publicstatic externint SE_shape_generate_point(int num_pts,SE_POINT[] point_array,double[] z,double[] measure, IntPtr shape_val);
调用:
lRc = SE_shape_generate_point(1, pnts,null, null, shape);
 
22、SE_shape_generate_buffer发生错误 

SE_INVALID_NUM_OF_PTS     (-36)

There are more points than allowed by the specified maximum points.

原因:是当一个面元图缓冲之后,比如原有四个点,缓冲之后,可能会产生100个点,而不是4个点 ,所以尽可能大

lRc = SE_shape_generate_buffer(ptrShape,1, 1000, ptrgShape);
 
23、指针 IntPtr[] 数组参数传递,如果
IntPtr[] columns =new IntPtr[1];
rv = SDETableGetColumnsInfo(m_SDEConn, m_SDEStream, sTableName,ref columns);
 
publicstatic int SDETableGetColumnsInfo(IntPtr pConn,IntPtr hQueryStream, string sTableName,refIntPtr[] destination)
 
如果数组在函数中进行了扩展,则必须传递ref,否则不回返回
 
24、SE_connection_create 每次连接都需要耗费时间,因此系统中连接可共享用一个。只到程序退出才关闭
if (m_SDEConn.ToInt32() > 0)return true;
SDEConnecttionFree(m_SDEConn);
//建立连接
//"10.2.25.12", "5151", "SDE", "sde", "ehl1234", ref pError, ref pSdeConn
SE_ERROR pError =new SE_ERROR();
rv = SDEAPIWrapper.SE_connection_create(pSDEParamSet.Server, pSDEParamSet.Instance, pSDEParamSet.Database, pSDEParamSet.User, pSDEParamSet.Password,ref pError, ref m_SDEConn);
//检查错误
 
25、sde91.dll 缓冲查询存在错误(wgs84坐标,缓冲半径为1),换为 sde.dll(实为9.2) 则正常
SDEAPIWrapper.SE_shape_generate_buffer(ptrShape, Radius, 1000, ptrgShape);
错误为:

SE_CORRIDOR_OUT_OF_BOUNDS     (-81)

The specified source shape and corridor distance would result in a corridor with coordinates that exceed the valid coordinate range.

26、char[] 是Unicode 字符 里面是用unicode保存

char 关键字用于声明下表所示范围内的 Unicode 字符。 Unicode 字符是 16 位字符,用于表示世界上大多数已知的书面语言。

类型

范围

Size

.NET Framework 类型

char

U+0000 到 U+ffff

16 位 Unicode 字符

System. Char

"中国".ToCharArray() 则 char[0]= 20013(中) char[1]= 22269 (国) 

27、数量必须保持一致

//插入的字段数量,必须在赋值处赋值,否则会出现-91错误
rv =SDEAPIWrapper.SE_stream_insert_table(hQueryStream, sTablename, Cols.Length, Cols);
 

SE_INVALID_INDICATOR_VALUE     (-91)

Returned during the execution of an insert or update operation for a column that was bound by reference (by calling SE_stream_bind_input_column) and the value of the indicator variable was neither IS_NOT_NULL_VALUE nor IS_NULL_VALUE.

28、日期

取出的日期 年需加+1900  月+1

赋值时,则 相反se_tm.tm_year=dValue.Year-1900; 月-1

29、形成查询条件:不同类型不同组成方法

 
GXSJ>= TO_DATE('2011-12-01 04:08:11','YYYY-MM-DD HH24:MI:SS')
 
"HH"= '中国人民武装警,察部队白银市支队'
 
DDD=0  DDD>='0'
 
中文字段需要"" 英文不需要,但加上也无错
 
30、SE_stream_set_nstring 乱码问题
 
定义

c中定义:

LONG SE_stream_set_nstring (SE_STREAM stream, SHORT column, constSE_WCHAR *string_val);

C#中定义:
publicstatic externint SE_stream_set_nstring(IntPtr stream,int column, string string_val);
 
应用:出现乱码
rc = SDEAPIWrapper.SE_stream_set_nstring(hQueryStream, dIndex, sValue);
 
定义:出乱码
publicstatic externint SE_stream_set_nstring(IntPtr stream,int column, StringBuilder string_val);
 
定义:正常
publicstatic externint SE_stream_set_nstring(IntPtr stream,int column, Byte[] string_val);
 
31、SE_stream_update_table 等字段列表 中中文的处理
Cols[0] = "DDD";
Cols[1] ="测试";
string Where_Clause ="OBJECTID = 5";
lRc = SE_stream_update_table(pStream,"K_C001_GAJG_PT", 2, Cols, Where_Clause);
 
后台监控:
SELECT MC,DZ,GXSJ,测 FROM  SDE.K_C001_GAJG_PT
Runtime error occurred: 904 (O)