1.Server端连接

            ESRI.ArcGIS.Server.GISServerConnectionClass gisconnection= new ESRI.ArcGIS.Server.GISServerConnectionClass();
            gisconnection.Connect("localhost");
            ESRI.ArcGIS.Server.IServerObjectManager som = gisconnection.ServerObjectManager;
            string servertype = "MapServer";
            string serverobjectname = "china";
            ESRI.ArcGIS.Server.IServerContext servercontext = som.CreateServerContext(serverobjectname, servertype);
            IMapServer ms = (IMapServer)servercontext.ServerObject;
            IMapServerObjects pMapServerObjs = ms as IMapServerObjects;
            IMap pMap = pMapServerObjs.get_Map(ms.DefaultMapName);
            for (int i = 0; i < pMap.LayerCount;i++ )
            {
                IFeatureLayer pFLayer = pMap.get_Layer(i) as IFeatureLayer;
                IFeatureClass pFeatureClass = pFLayer.FeatureClass;
                Console.WriteLine(pFeatureClass.FeatureCount(null).ToString());
            }
            servercontext.ReleaseContext();

2. Engine连接Server

(1)    IAGSServerObjectName pServerObjectName = GetMapServer("机器名或者URL", "china", true);
            IName pName = (IName)pServerObjectName;
            //访问地图服务
            IMapServer pMapServer = (IMapServer)pName.Open();


            ESRI.ArcGIS.Carto.IMapServerLayer pMapServerLayer = new ESRI.ArcGIS.Carto.MapServerLayerClass();
            //连接地图服务

            pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);


            axMapFull.AddLayer(pMapServerLayer as ILayer);//这里会把服务中的图层合成一个图层来显示,虽然看起来

            //和服务中的地图一样

            axMapFull.Refresh();

(2)

        public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {

            //设置连接属性
            IPropertySet pPropertySet = new PropertySetClass();

            if (pIsLAN)
                pPropertySet.SetProperty("machine", pHostOrUrl);//machine可以是机器名或者IP,连接方式为LAN。
            else
                pPropertySet.SetProperty("url", pHostOrUrl);//如果url连接的需要设置用户名和密码,那么要设置用户名和密码属性。

             //如果使用url连接,那么一定要在浏览器可以访问才行。


            //打开连接
           
            IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();

            IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);

            //Get the image server.
            IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
            pServerObjectNames.Reset();
            IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
            while (ServerObjectName != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
                    (ServerObjectName.Type == "MapServer"))
                {

                    break;
                }
                ServerObjectName = pServerObjectNames.Next();
            }

            //返回对象
            return ServerObjectName;
        }

3.ADF连接
            ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("user", "passwd", "domain");
            ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection ags_connection;
            agsconnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("hostname", identity);
            agsconnection.Connect();
            IServerObjectManager som = agsconnection.ServerObjectManager;