分享

C#+AE+SQL 增、删、改、查

 秋寒月 2010-11-29

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using ESRI.ArcGIS.Geometry;      
using ESRI.ArcGIS.Display;      
using ESRI.ArcGIS.Carto;        
using ESRI.ArcGIS.Controls;

using System.Globalization;  //时间

namespace GIS_Demo1
{
    public partial class DataFrm : Form
    {
        public string str = null;
        public string id0 = null;
        public int c1 = 0;
        public int c2 = 1;
        private static string ConnectionString = "Data Source=MICROSOF-44BEF0;Initial Catalog=追踪;user id=sa;password=zzw848686;";
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter sda = null;
        public IMapControl2 pMapControl;
        public IMap pMap;

        public DataFrm(IMapControl2 pFMapControl)
        {
            pMapControl = pFMapControl;
            pMap = pFMapControl.Map;
            InitializeComponent();
            conn = new SqlConnection(ConnectionString);
        }

        //查看数据表
        private void button1_Click(object sender, EventArgs e)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            string sql = "select * from Tracker";
            try
            {
                PictMarkerSymbol(sql);
            }
            catch
            {

            }
        }

        //加入图标
        public void PictMarkerSymbol(string str1)
        {
            string sql = str1;
            DataSet ds = new DataSet();
            cmd = new SqlCommand(sql, conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            IActiveView pActView = (IActiveView)pMap;
            pMap.ClearSelection();
            pActView.Refresh();
            IGraphicsContainer pGraphContainer = (IGraphicsContainer)pActView;
            pGraphContainer.DeleteAllElements();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                double EnterpX, EnterpY;
                try
                {
                    EnterpX = Convert.ToDouble(ds.Tables[0].Rows[i]["Latitude"]);
                    EnterpY = Convert.ToDouble(ds.Tables[0].Rows[i]["Longitude"]);
                }
                catch
                {
                    return;
                }
                IPoint pPoint = new PointClass();
                pPoint.PutCoords(EnterpX, EnterpY);
                IPictureMarkerSymbol pPicturemksb = new PictureMarkerSymbolClass();
                pPicturemksb.Size = 20;
                pPicturemksb.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, "E:\\资料文档\\GIS Demo1\\image\\LabelLayerCmd.bmp");
                IMarkerElement pMarkerEle = new MarkerElementClass();
                pMarkerEle.Symbol = pPicturemksb as IMarkerSymbol;
                IElement pEle = (IElement)pMarkerEle;
                pEle.Geometry = pPoint;
                //pMapControl.CenterAt(pPoint);
                pGraphContainer.AddElement(pEle, 0);
                pActView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }

        }
       
        //增加一条新记录
        private void button2_Click(object sender, EventArgs e)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            DataSet ds = new DataSet();
            string sql2 = "select * from Tracker";
            cmd = new SqlCommand(sql2, conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            int co = ds.Tables[0].Rows.Count;
            int ct = Convert.ToInt32(ds.Tables[0].Rows[co - 1]["id"]);
            int cout = ct + 1;
            string sql = "insert into Tracker([id],[TrackerNumber]) values(" + cout.ToString() + ",null)";
            try
            {
                cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                string sql1 = "select * from Tracker";
                PictMarkerSymbol(sql1);
            }
            catch
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            IMap pMap = pMapControl.Map;
            IActiveView pActiveView = (IActiveView)pMap;
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)pActiveView;
            pGraphicsContainer.DeleteAllElements();
            pMap.ClearSelection();
            pActiveView.Refresh();
        }

        //修改记录
        private void button4_Click(object sender, EventArgs e)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            string sql = null;
            if (c1 == 1)
            {
                sql = "update Tracker set [name]='" + this.textBox1.Text + "' where id=" + id0;
            }
            if (c1 == 2)
            {
                sql = "update Tracker set Latitude=" + this.textBox1.Text + " where id=" + id0;
            }
            if (c1 == 3)
            {
                sql = "update Tracker set Longitude=" + this.textBox1.Text + " where id=" + id0;
            }
            //string sql = "update Tracker set "+colun+"='" + this.textBox1.Text + "' where id=" + id0;
            try
            {
                cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                string sql1 = "select * from Tracker";
                PictMarkerSymbol(sql1);
            }
            catch
            {
                MessageBox.Show("输入数据类型错误,请重新输入!");
                return;
            }
        }

        //删除记录
        private void button3_Click_1(object sender, EventArgs e)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            string str1 = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "delete from Tracker where id=" + str1;
            try
            {
                cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                string sql1 = "select * from Tracker";
                PictMarkerSymbol(sql1);
            }
            catch
            {

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {

            string sql = "select * from Tracker";
            DataSet ds = new DataSet();
            cmd = new SqlCommand(sql, conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                double EnterpX, EnterpY;
                try
                {
                    EnterpX = Convert.ToDouble(ds.Tables[0].Rows[i]["Latitude"]);
                    EnterpY = Convert.ToDouble(ds.Tables[0].Rows[i]["Longitude"]);
                }
                catch
                {
                    return;
                }
                if (c2 == i)
                {
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(EnterpX, EnterpY);
                    pMapControl.CenterAt(pPoint);
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            c1 = e.ColumnIndex;
            if (c1 == -1)
            {
                c1 = c1 + 1;
            }
            c2 = e.RowIndex;
            DataGridViewCell dgvc = (DataGridViewCell)(this.dataGridView1.Rows[c2].Cells[c1]);
            id0 = ((DataGridViewCell)(this.dataGridView1.Rows[c2].Cells[0])).Value.ToString();
            str = dgvc.Value.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            IMap pMap = pMapControl.Map;
            IActiveView pActiveView = (IActiveView)pMap;
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)pActiveView;
            pGraphicsContainer.DeleteAllElements();
            pMap.ClearSelection();
            pActiveView.Refresh();


            string str1 = "select * from Tracker";
            string sql = str1;
            DataSet ds = new DataSet();
            cmd = new SqlCommand(sql, conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            IActiveView pActView = (IActiveView)pMap;
            pMap.ClearSelection();
            pActView.Refresh();
            IGraphicsContainer pGraphContainer = (IGraphicsContainer)pActView;
            pGraphContainer.DeleteAllElements();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                double EnterpX, EnterpY;
                try
                {
                    EnterpX = Convert.ToDouble(ds.Tables[0].Rows[i]["Latitude"]);
                    EnterpY = Convert.ToDouble(ds.Tables[0].Rows[i]["Longitude"]);
                }
                catch
                {
                    return;
                }
                if (i == ds.Tables[0].Rows.Count-1)
                {
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(EnterpX, EnterpY);
                    IPictureMarkerSymbol pPicturemksb = new PictureMarkerSymbolClass();
                    pPicturemksb.Size = 20;
                    pPicturemksb.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, "E:\\资料文档\\GIS Demo1\\image\\LabelLayerCmd.bmp");
                    IMarkerElement pMarkerEle = new MarkerElementClass();
                    pMarkerEle.Symbol = pPicturemksb as IMarkerSymbol;
                    IElement pEle = (IElement)pMarkerEle;
                    pEle.Geometry = pPoint;
                    //pMapControl.CenterAt(pPoint);
                    pGraphContainer.AddElement(pEle, 0);
                    pActView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                }
                else
                {
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(EnterpX, EnterpY);
                    IPictureMarkerSymbol pPicturemksb = new PictureMarkerSymbolClass();
                    pPicturemksb.Size = 20;
                    pPicturemksb.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, "E:\\资料文档\\GIS Demo1\\image\\LabelLayerCmd.bmp");
                    IMarkerElement pMarkerEle = new MarkerElementClass();
                    pMarkerEle.Symbol = pPicturemksb as IMarkerSymbol;
                    IElement pEle = (IElement)pMarkerEle;
                    pEle.Geometry = pPoint;
                    //pMapControl.CenterAt(pPoint);
                    pGraphContainer.AddElement(pEle, 0);
                    pActView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                    //延时                  
                    System.Threading.Thread.Sleep(1000);  

                    pGraphicsContainer.DeleteAllElements();
                    pMap.ClearSelection();
                    pActiveView.Refresh();
                                      
            }
        }

       ////延时                  
       //System.Threading.Thread.Sleep(1000);  
    }
}
C#+AE+SQL <wbr>增、删、改、查

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多