⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 services.cs

📁 微软的行业应用解决方案示例
💻 CS
📖 第 1 页 / 共 4 页
字号:
        {
            //Creat a LatLong array 
            LatLong[] _latlongs = new LatLong[2];
            //Add the starting location
            _latlongs[0] = new LatLong();
            _latlongs[0] = Start.LatLong;
            //Add the ending location
            _latlongs[1] = new LatLong();
            _latlongs[1] = End.LatLong;

            //Create a MapOptions object
            MapOptions _options = new MapOptions();
            //Set the MapOptions' properties
            _options.Format = new ImageFormat();
            _options.Format.Height = height;
            _options.Format.Width = width;

            //Return a North American route based on the start and end lat and long
            Route _route = route.CalculateSimpleRoute(_latlongs, "MapPoint.NA", SegmentPreference.Quickest);

            //Create a MapSpecification object
            MapSpecification _mapSpec = new MapSpecification();
            //Set the MapSpecification's properties
            _mapSpec.DataSourceName = "MapPoint.NA";
            _mapSpec.Options = _options;
            _mapSpec.Route = _route;

            //Call the MapPoint web service to return an array of maps
            MapImage[] _images = render.GetMap(_mapSpec);

            //Return the first map as a Bitmap object
            return new Bitmap(new MemoryStream(_images[0].MimeData.Bits));
        }


        /// <summary>
        /// Retrieve a route map in Bitmap form
        /// </summary>
        /// <param name="height">height of the image</param>
        /// <param name="width">with of the image</param>
        /// <param name="address">address to display</param>
        /// <param name="city">city of the address</param>
        /// <param name="state">state of the address</param>
        /// <returns>Map image with the address displayed</returns>
        public static Bitmap GetRouteMap(int height, int width, string address, string city, string state)
        {
            //Pass the address and PictureBox dimensions to retrieve a map bitmap
            return CallMapPoint(height, width, address, city, state);
        }


        /// <summary>
        /// Retrieve a Collection of Truck objects
        /// </summary>
        /// <param name="orderId">Order being loaded</param>
        /// <returns>Truck to be loaded</returns>
        public static Truck GetTruckToLoad(Guid orderId)
        {
            List<Truck> listTrucks;
            Parameters parameters;
            Truck result = null;

            // setup parameters for SQL
            parameters = new Parameters();
            parameters.Add("@orderId", orderId);

            // Retrieve trucks associated with an order
            // this could have had a where clause rather than use SQL.
            // if you did to boost performance use table direct and set an 
            // index with the value or range.
            var truckQuery = from currentRow in _dataStore.Read(StoredProcs.GetTruckLoad, parameters)
                             select new Truck(GetValue<Nullable<int>>(currentRow[Truck.TRUCKID_COLUMN]),
                                              GetValue<string>(currentRow[Truck.LICENSEPLATESTATEPROVINCE_COLUMN]),
                                              GetValue<string>(currentRow[Truck.LICENSEPLATENUMBER_COLUMN]),
                                              GetValue<Nullable<int>>(currentRow[Truck.ROUTEID_COLUMN]));

            listTrucks = truckQuery.ToList();

            // Make sure a truck was found.
            if (listTrucks.Count > 0)
            {
                result = listTrucks.ElementAt(0);
            }

            return result;
        }


        /// <summary>
        /// This method allows for safe setting of a byte array from the 
        /// database. The value could be null.
        /// </summary>
        /// <typeparam name="T">type to be converted</typeparam>
        /// <param name="newValue">new value</param>
        /// <returns>a type or null</returns>
        private static T GetValue<T>(object newValue)
        {
            T result = default(T);

            // Check to see if value is null or not
            if ((newValue != null) && (newValue != DBNull.Value))
            {
                result = (T)newValue;
            }

            return result;
        }


        /// <summary>
        /// Retrieves the application settings from the configuration xml
        /// file and loads them into the Global Cache for use throughout the app
        /// </summary>
        public static void LoadAppSettings()
        {
            GlobalCache.Instance.InternetUrl = GetAppSetting("InternetUrl");
            GlobalCache.Instance.InternetLogin = GetAppSetting("InternetLogin");
            GlobalCache.Instance.InternetPassword = GetAppSetting("InternetPassword");
            GlobalCache.Instance.Publisher = GetAppSetting("Publisher");
            GlobalCache.Instance.PublisherDatabase = GetAppSetting("PublisherDatabase");
            GlobalCache.Instance.Publication = GetAppSetting("Publication");
            GlobalCache.Instance.Subscriber = GetAppSetting("Subscriber");
            GlobalCache.Instance.CompressionLevel = short.Parse(GetAppSetting("CompressionLevel"));
            GlobalCache.Instance.SqlCeDatabase = GetAppSetting("SQLEVDatabase");
            GlobalCache.Instance.SqlCePassword = GetAppSetting("SQLEVPassword");
            GlobalCache.Instance.MapPointUserName = GetAppSetting("MapPointUserName");
            GlobalCache.Instance.MapPointPassword = GetAppSetting("MapPointPassword");
            GlobalCache.Instance.MapPointFindServiceUrl = GetAppSetting("MapPointFindServiceUrl");
            GlobalCache.Instance.MapPointRenderServiceUrl = GetAppSetting("MapPointRenderServiceUrl");
            GlobalCache.Instance.MapPointRouteServiceUrl = GetAppSetting("MapPointRouteServiceUrl");
        }


        /// <summary>
        /// Retrieves device and application information and load it into
        /// the Global Cache for use in the reporting of metrics
        /// </summary>
        public static void LoadDeviceData()
        {
            //Get Application Path
            GlobalCache.Instance.AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\";

            //Get Configuration File
            GlobalCache.Instance.ConfigFile = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase + ".config";

            //Get Device Name
            GlobalCache.Instance.DeviceName = System.Net.Dns.GetHostName();

            //Get IP Address
            IPHostEntry _ipHost = System.Net.Dns.GetHostEntry(GlobalCache.Instance.DeviceName);
            GlobalCache.Instance.IpAddress = _ipHost.AddressList[0].ToString();

            //Get Operating System Name
            GlobalCache.Instance.OperatingSystem = System.Environment.OSVersion.Platform.ToString();

            //Get Operating System Version
            GlobalCache.Instance.OperatingSystemVersion = System.Environment.OSVersion.Version.ToString();

            //Get Compact Framework Version
            GlobalCache.Instance.NetCFVersion = System.Environment.Version.ToString();

            //Get App Version
            GlobalCache.Instance.AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            //Get Unique Device ID
            GlobalCache.Instance.DeviceId = Native.GetDeviceID("HardwareDistributor");
        }


        /// <summary>
        /// Logs Application State
        /// </summary>
        public static void LogAppState()
        {
            StreamWriter _sw = null;

            try
            {
                //Get physical and available device memory
                uint _storePages = 0;
                uint _ramPages = 0;
                uint _pageSize = 0;
                int _res = Native.GetSystemMemoryDivision(ref _storePages, ref _ramPages, ref _pageSize);
                Native.MEMORYSTATUS _memStatus = new Native.MEMORYSTATUS();
                Native.GlobalMemoryStatus(_memStatus);
                uint _total = _memStatus.dwTotalPhys / 1024;
                uint _available = _memStatus.dwAvailPhys / 1024;

                //Instantiate an appendable streamwriter that writes to a file called AppStateLog.txt
                _sw = new StreamWriter(GlobalCache.Instance.AppPath + "AppStateLog.txt", true, System.Text.Encoding.ASCII);
                //Write a row of data containing the time plus the follow device metrics
                _sw.WriteLine("* " + System.DateTime.Now.ToString() + " | " +
                              GlobalCache.Instance.DeviceId + " | " +
                              GlobalCache.Instance.DeviceName + " | " +
                              GlobalCache.Instance.IpAddress + " | " +
                              GlobalCache.Instance.OperatingSystem + " | " +
                              GlobalCache.Instance.OperatingSystemVersion + " | " +
                              GlobalCache.Instance.NetCFVersion + " | " +
                              GlobalCache.Instance.AppVersion + " | " +
                              SystemState.PowerBatteryStrength.ToString() + " | " +
                              _available.ToString() + " | " +
                              _total.ToString());
            }
            finally
            {
                //Ensure the streamwrite gets closed
                if (_sw != null)
                {
                    _sw.Close();
                    _sw = null;
                }
            }
        }


        /// <summary>
        /// Logs errors
        /// </summary>
        /// <param name="ex">exception to be logged</param>
        public static void LogErrors(Exception ex)
        {
            StreamWriter _sw = null;

            try
            {
                //Instantiate an appendable streamwriter that writes to a file called ErrorLog.txt
                _sw = new StreamWriter(GlobalCache.Instance.AppPath + "ErrorLog.txt", true, System.Text.Encoding.ASCII);
                //Write a row of data containing the time, the error message, and the stack trace
                _sw.WriteLine("* " + System.DateTime.Now.ToString() + " | " + ex.Message + " | " + ex.StackTrace);
            }
            finally
            {
                //Ensure the streamwriter gets closed
                if (_sw != null)
                {
                    _sw.Close();
                    _sw = null;
                }
            }
        }


        /// <summary>
        /// Inserts, updates or deletes an order.
        /// </summary>
        /// <param name="order">order that has changed</param>
        public static void SaveOrder(Order order)
        {
            Parameters parameters;

            try
            {
                _dataStore.BeginTransaction();

                // Create order header
                if (order.ObjectState != ObjectState.Unchanged)
                {
                    parameters = new Parameters();
                    _currentOrder = order; // setup for trigger

                    switch (order.ObjectState)
                    {
                        case ObjectState.Delete:
                            parameters.Add("@OrderId", order.OrderId);
                            _dataStore.Save(StoredProcs.DeleteOrderDetails, parameters);
                            _dataStore.Save(StoredProcs.DeleteOrder, parameters);
                            break;
                        case ObjectState.Modify:
                            parameters.Add("@OrderId", order.OrderId);
                            parameters.Add("@CustomerId", order.Customer.CustomerId);
                            parameters.Add("@DeliveryDate", order.DeliveryDate);
                            parameters.Add("@OrderState", order.OrderState);
                            // Can't just add the order signature here since we may not even have it yet
                            // either that or this query needs to take null as a parameter for signature
                            // parameters.Add("@Signature", order.Signature);
                            _dataStore.Save(StoredProcs.UpdateOrder, parameters);
                            break;
                        case ObjectState.New:
                            //get new order guid
                            order.OrderId = Guid.NewGuid();

                            //get new display id.
                            order.DisplayId = GetNewDisplayId();
                            parameters.Add("@OrderId", order.OrderId);
                            parameters.Add("@CustomerId", order.CustomerId);
                            parameters.Add("@DeliveryDate", order.DeliveryDate);
                            parameters.Add("@OrderState", order.OrderState);
                            parameters.Add("@DisplayId", order.DisplayId);
                            _dataStore.Save(StoredProcs.CreateOrder, parameters);

                            //Retrieve value of Identity column that was just incremented
                            //order.OrderId = Convert.ToInt32(_dataStore.GetValue(StoredProcs.GetIdentity"));

                            break;
                    }
                }

                order.ObjectState = ObjectState.Unchanged;
                if (order.ObjectState != ObjectState.Delete)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -