📄 services.cs
字号:
using System;
using System.Net;
using System.IO;
using HardwareDistributor.Data;
using Microsoft.WindowsMobile.Status;
using System.Data.SqlServerCe;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace HardwareDistributor.Business
{
/// <summary>
/// Enum to easily work with
/// various Order states
/// </summary>
public enum OrderState
{
Cancelled = 0,
Placed = 1,
Picked = 2,
Loaded = 3,
OutForDelivery = 4,
Delivered = 5,
All = 6
}
/// <summary>
/// Business layer Services made available for
/// consumption by the UI layer
/// </summary>
public class Services
{
/// <summary>
/// Informs the caller of the device's current network
/// connection status in order to make networking decisions
/// </summary>
/// <returns></returns>
public static bool GetConnectionStatus()
{
//See if there's one or more network connections
if (SystemState.ConnectionsCount > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Test to see if the replication ISAPI DLL is reachable
/// </summary>
/// <returns></returns>
public static bool CheckReplicationUrl()
{
//Create a NetworkCredential object
NetworkCredential cred = new NetworkCredential(GlobalCache.Instance.InternetLogin, GlobalCache.Instance.InternetPassword);
//Pass the ISAPI DLL URL and the credentials to webcheck
return Data.NetAccess.webCheck(GlobalCache.Instance.InternetUrl, cred);
}
/// <summary>
/// Tests to see if the SQL Server Everywhere database exists
/// </summary>
/// <returns></returns>
public static bool DatabaseExists()
{
//Pass the application path plus the database name to the Exists method
if (File.Exists(GlobalCache.Instance.AppPath + GlobalCache.Instance.SqlEvDatabase))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Deletes a SQL Server Everywhere database
/// </summary>
public static void DeleteDatabase()
{
File.Delete(GlobalCache.Instance.AppPath + GlobalCache.Instance.SqlEvDatabase);
}
/// <summary>
/// Logs errors
/// </summary>
/// <param name="ex"></param>
public static void LogErrors(Exception ex)
{
Data.FileAccess.LogError(ex);
}
/// <summary>
/// Logs Application State
/// </summary>
/// <param name="ex"></param>
public static void LogAppState()
{
Data.FileAccess.LogAppState();
}
/// <summary>
/// Synchronize with a remote data source
/// </summary>
/// <param name="ex"></param>
public static void SyncWithDataSource()
{
Data.Database.Synchronize();
}
/// <summary>
/// Caches a data source connection
/// </summary>
/// <param name="ex"></param>
public static void CacheDataSourceConnection()
{
Data.Database.AddConnectionToCache();
}
/// <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 = Data.FileAccess.GetAppSetting("InternetUrl");
GlobalCache.Instance.InternetLogin = Data.FileAccess.GetAppSetting("InternetLogin");
GlobalCache.Instance.InternetPassword = Data.FileAccess.GetAppSetting("InternetPassword");
GlobalCache.Instance.Publisher = Data.FileAccess.GetAppSetting("Publisher");
GlobalCache.Instance.PublisherDatabase = Data.FileAccess.GetAppSetting("PublisherDatabase");
GlobalCache.Instance.Publication = Data.FileAccess.GetAppSetting("Publication");
GlobalCache.Instance.Subscriber = Data.FileAccess.GetAppSetting("Subscriber");
GlobalCache.Instance.CompressionLevel = short.Parse(Data.FileAccess.GetAppSetting("CompressionLevel"));
GlobalCache.Instance.SqlEvDatabase = Data.FileAccess.GetAppSetting("SQLEVDatabase");
GlobalCache.Instance.SqlEvPassword = Data.FileAccess.GetAppSetting("SQLEVPassword");
GlobalCache.Instance.MapPointUserName = Data.FileAccess.GetAppSetting("MapPointUserName");
GlobalCache.Instance.MapPointPassword = Data.FileAccess.GetAppSetting("MapPointPassword");
GlobalCache.Instance.MapPointFindServiceUrl = Data.FileAccess.GetAppSetting("MapPointFindServiceUrl");
GlobalCache.Instance.MapPointRenderServiceUrl = Data.FileAccess.GetAppSetting("MapPointRenderServiceUrl");
GlobalCache.Instance.MapPointRouteServiceUrl = Data.FileAccess.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
byte[] _id = getDeviceID("HardwareDistributor");
string _deviceId = string.Empty;
foreach (byte _b in _id)
{
_deviceId = String.Format("{0}{1:X2} ", _deviceId, _b);
}
GlobalCache.Instance.DeviceId = _deviceId;
}
/// <summary>
/// Retrieve a Collection of Role objects
/// </summary>
/// <returns></returns>
public static RoleCollection GetRoles()
{
SqlCeCommand _cmd = null;
try
{
Role _role = null;
object[] _obj = null;
//Create a RoleCollection to be filled with Role Objects
RoleCollection _roles = new RoleCollection();
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "SELECT RoleId, Role FROM Roles";
//Execute query and fill ArrayList with results
ArrayList _list = Data.Database.ReadValues(_cmd, 0);
//Iterate through the ArrayList
for (int i = 0; i < _list.Count; i++)
{
//Create a Role object
_role = new Role();
//Set object array equal to row
//of objects found in ArrayList
_obj = (object[])_list[i];
//Get RoleId from column 0
_role.RoleId = Convert.ToInt32(_obj[0]);
//Check for null
if (_obj[1] != DBNull.Value)
{
//Get RoleName from column 1
_role.RoleName = Convert.ToString(_obj[1]).Trim();
}
//Add hydrated Role Object to Roll Collection
_roles.Add(_role);
}
//Return Role Collection to UI
return _roles;
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Retrieve a Collection of Customer objects
/// </summary>
/// <returns></returns>
public static CustomerCollection GetCustomers()
{
SqlCeCommand _cmd = null;
try
{
Customer _customer = null;
object[] _obj = null;
//Create a CustomerCollection to be filled with Customer objects
CustomerCollection _customers = new CustomerCollection();
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "SELECT CustomerId, Name, StreetAddress, City, StateProvince, PostalCode, ContactName, ContactPhone, RouteId FROM Customers";
//Execute query and fill ArrayList with results
ArrayList _list = Data.Database.ReadValues(_cmd, 0);
//Iterate through the ArrayList
for (int i = 0; i < _list.Count; i++)
{
//Create a Customer object
_customer = new Customer();
//Set object array equal to row
//of objects found in ArrayList
_obj = (object[])_list[i];
//Get CustomerId from column 0
_customer.CustomerId = Convert.ToInt32(_obj[0]);
//Check for null
if (_obj[1] != DBNull.Value)
{
//Get CustomerName from column 1
_customer.CustomerName = Convert.ToString(_obj[1]).Trim();
}
//Check for null
if (_obj[2] != DBNull.Value)
{
//Get StreetAddress from column 2
_customer.StreetAddress = Convert.ToString(_obj[2]).Trim();
}
//Check for null
if (_obj[3] != DBNull.Value)
{
//Get City from column 3
_customer.City = Convert.ToString(_obj[3]).Trim().Trim();
}
//Check for null
if (_obj[4] != DBNull.Value)
{
//Get StateProvince from column 4
_customer.StateProvince = Convert.ToString(_obj[4]).Trim();
}
//Check for null
if (_obj[5] != DBNull.Value)
{
//Get PostalCode from column 5
_customer.PostalCode = Convert.ToString(_obj[5]).Trim();
}
//Check for null
if (_obj[6] != DBNull.Value)
{
//Get ContactName from column 6
_customer.ContactName = Convert.ToString(_obj[6]).Trim();
}
//Check for null
if (_obj[7] != DBNull.Value)
{
//Get ContactPhone from column 7
_customer.ContactPhone = Convert.ToString(_obj[7]).Trim();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -