To add a point object to a vector layer based on data in SQL Server

Source: Internet
Author: User

    1. Data in SQL Server already has coordinate information (LAT,LNG)
    2. Gets the data from SQL in the Formload event and stores it in the dataset.
 //defining SqlConnection objects at the top//Public static string constr = "server= (local);d atabase=dbname;uid=sa;pwd=123";//Public SqlConnection conn = new SqlConnection (CONSTR);Conn. Open ();//Open a database connection            if(Conn. State! =ConnectionState.Open) {MessageBox.Show ("Database connection Failed! "); } SqlCommand cmd=NewSqlCommand ("write a SELECT statement here (filter for invalid data)", conn); SqlDataAdapter SDA=NewSqlDataAdapter (); Sda. SelectCommand=cmd; DataSet DS=NewDataSet (); Sda. Fill (DS,"AQI"); DataTable Tblaqi= ds. tables["AQI"];//AQI TableDatagridview1.datasource =ds; Datagridview1.datamember="AQI"; Conn.            Close (); 

3. Open the workspace and call the Pointeditor method

//Layer EditingIMap PMAP2 =Axmapcontrol1.map; PLayer=NULL; PLayer= Pmap2.get_layer (0);//Get target layerIfeaturelayer Pfeaturelayer = PLayer asIfeaturelayer; Ifeatureclass Pfeatureclass=Pfeaturelayer.featureclass; //Open Edit SpaceIworkspace IW =((Idataset) pfeatureclass).            Workspace; Iworkspaceedit IWE= IW asIworkspaceedit; BOOLStartEdit =IWE.            Isbeingedited (); if(!StartEdit) {IWE. Startediting (false); } IWE.            Starteditoperation (); //Add site features             for(intj =0; J < TblAQI.Rows.Count; J + +) {Levelinfo leinfo=NewLevelinfo ();//structural Levelinfo for temporary storage of dataLeinfo. Name2 = tblaqi.rows[j][ +].                ToString (); Leinfo. Loacx=float. Parse (tblaqi.rows[j][Ten].                ToString ()); Leinfo. Loacy=float. Parse (tblaqi.rows[j][9].                ToString ()); Leinfo. AQI=float. Parse (tblaqi.rows[j][0].                ToString ()); Leinfo.code= tblaqi.rows[j][ -].                ToString (); Leinfo. level=Fenji (Leinfo.                AQI); Pointeditor (Leinfo.code, Leinfo. Level, Pfeatureclass, Leinfo. Loacx, Leinfo. Loacy, Leinfo.                NAME2); PointEditor2 (Leinfo.code, Leinfo.                AQI, Pfeatureclass); Iactiveview Pactiveview= PMap2 asIactiveview;            Pactiveview.refresh (); } Fenjizs ();//Graded RenderingIWE.            Stopeditoperation (); StartEdit=IWE.            Isbeingedited (); if(!StartEdit) {IWE. StopEditing (true); } axmapcontrol1.refresh ();

Pointeditor method

  Public voidPointeditor (stringNameto,intValuetoset, Ifeatureclass Pfeatureclass,floatXfloatYstringNAME2)//ways to modify the Aqitype property        {            Try{ifields pFields1=Pfeatureclass.fields; Iqueryfilter QueryFilter=NewQueryfilterclass ();Queryfilter.whereclause ="code = '"+ Nameto +"'";//Field properties of the queryIfeaturecursor featurecursor = Pfeatureclass.search (QueryFilter,false); IFeature feature=Featurecursor.                Nextfeature (); Feature.set_value (Pfields1.findfield ("Aqitype"), Valuetoset); Feature.                Store ();            System.Runtime.InteropServices.Marshal.ReleaseComObject (Featurecursor); }            Catch{IFeature pfeature; Ifeatureclasswrite PFEATURECLASSW= Pfeatureclass asIfeatureclasswrite; IPoint PPoint=NewPointclass (); Ppoint.spatialreference= This. axmapcontrol1.spatialreference;                Ppoint.putcoords (x, y);                Igeometry Pgeo; Pgeo=PPoint; Pfeature=pfeatureclass.createfeature (); Pfeature.shape=Pgeo; Pfeature.set_value (5, Nameto); Pfeature.set_value (7, Valuetoset); Pfeature.set_value (6, name2);            Pfeature.store (); }        }

PointEditor2 method

 Public voidPointEditor2 (stringNameto,floatValuetoset, Ifeatureclass Pfeatureclass)//ways to modify AQI properties        {            if(Valuetoset = =-999) {Valuetoset=1; } ifields pFields1=Pfeatureclass.fields; Iqueryfilter QueryFilter=NewQueryfilterclass ();Queryfilter.whereclause ="code = '"+ Nameto +"'";//Field properties of the queryIfeaturecursor featurecursor = Pfeatureclass.search (QueryFilter,false); IFeature feature=Featurecursor.            Nextfeature (); Feature.set_value (Pfields1.findfield ("AQI"), Valuetoset); Feature.            Store ();        System.Runtime.InteropServices.Marshal.ReleaseComObject (Featurecursor); }

To add a point object to a vector layer based on data in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.