Data | database
mapx read data from the database to form a new layer (C #) 2005-5-10 implementation of MAPX in C # reading data from a database to form a new layer is divided into two problems:1. MAPX reads data from the database to form a new layer;2. transforms the DataTable into an ADO recordset. The second problem here is caused by the first problem, because MAPX is a COM control, and it only supports ADO data access, while C # programming typically uses the ado.net approach, which requires a conversion between the two ways. (Of course, you can also use ADO in C #, but this is a backward method.) This workaround can refer to the article "Accessing the database using ADO in Visual Basic.NET"). For the solution to the second question, see the author's other article, "Converting a DataTable object to a Recordset object" (http://blog.csdn.net/allentao/archive/2005/05/11/373634). aspx), this article only describes how to solve the first problem after you get the Recordset object. The steps below:1. create the Cmapxfields object and Add fields to the fields in the database;2. Create a Cmapxbindlayer object that specifies the ordinal of its coordinate value field;3. to map. Add a dataset to the datasets to generate a new layer;4. specify the display style of the elements in the new layer, using the display bitmap, This requires that the bitmap to be displayed be placed under the Custsymb folder of the MAPX installation directory.
The code is as follows: private void Creatnewlayerfromdb (String layername, ADODB. Recordset rsnopass) {this. Deletelayerbyname (LayerName); Remove an existing layer
Cmapxfields flds=new Fieldsclass ();
//Describe The structure of the unbound dataset FLDS. ADD ("Stationid", "Theid", aggregationfunctionconstants.miaggregationindividual, fieldtypeconstants.mitypestring); FLDS. ADD ("Address", "address", Aggregationfunctionconstants.miaggregationindividual, fieldtypeconstants.mitypestring); FLDS. ADD ("Longitude", "longitude", Aggregationfunctionconstants.miaggregationsum, fieldtypeconstants.mitypenumeric); //Longitude FLDS. ADD ("Latitude", "latitude", Aggregationfunctionconstants.miaggregationsum, fieldtypeconstants.mitypenumeric); //Latitude
cmapxbindlayer bindlayerobject=new bindlayerclass (); bindlayerobject.layername= layername; bindlayerobject.refcolumn1=3; bindlayerobject.refcolumn2=4; Bindlayerobject.layertype=bindlayertypeconstants.mibindlayertypexy;
Cmapxdataset Dataset=map. Datasets.add (Datasettypeconstants.midatasetado, Rsnopass , LayerName, "Stationid", "Address", Bindlayerobject, FLDS, false);
Cmapxlayer Layer=map. Layers._item (LayerName); layer. Overridestyle= true; string Picname= "icon. BMP "; if (layer. Style.supportsbitmapsymbols = = true) { layer. Style.symboltype = symboltypeconstants.misymboltypebitmap; layer. Style.symbolbitmapsize = 60; layer. Style.symbolbitmaptransparent = true; layer. Style.symbolbitmapname = picname; } }
private void Deletelayerbyname (string layername) { int Count=map. layers.count; for (int i=1; i<count; i++) { if (map. Layers._item (i). Name==layername) map. Layers.remove (i); } }
Description: The number of the MAPX control is starting from 1, which can be seen from the layers collection and the serial number of the fields set in the above code.