📄 services.cs
字号:
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Verify a user's login credentials to either
/// grant or deny access to the system
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool VerifyCredentials(string userName, string password)
{
SqlCeCommand _cmd = null;
try
{
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "SELECT COUNT(*) FROM Employees WHERE UserName = @UserName AND Password = @Password";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@UserName", userName);
_cmd.Parameters.AddWithValue("@Password", password);
//Retrieve number of users that match the supplied credentials
int count = (int)Data.Database.ReadSingleValue(_cmd);
//If none match, return false and deny access
if (count == 0)
{
return false;
}
else
{
return true;
}
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Create a new Order and associated OrderDetails
/// </summary>
/// <param name="customerId"></param>
/// <param name="deliveryDate"></param>
/// <param name="orderState"></param>
/// <param name="orderItems"></param>
public static void CreateOrder(int customerId, string deliveryDate, int orderState, ListView.ListViewItemCollection orderItems)
{
SqlCeCommand _cmd = null;
try
{
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "INSERT INTO Orders (CustomerId, DeliveryDate, OrderState) VALUES (@CustomerId, @DeliveryDate, @OrderState)";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@CustomerId", customerId);
_cmd.Parameters.AddWithValue("@DeliveryDate", deliveryDate);
_cmd.Parameters.AddWithValue("@OrderState", orderState);
//Insert new Order into database
Data.Database.InsertUpdateDelete(_cmd);
//Set the SQL statement
_cmd.CommandText = "SELECT @@IDENTITY";
//Retrieve value of Identity column that was just incremented
int _identity = Convert.ToInt32(Data.Database.ReadSingleValue(_cmd));
//Iterate through collection
foreach (ListViewItem item in orderItems)
{
//Set the SQL statement
_cmd.CommandText = "INSERT INTO OrderDetails (OrderId, InventoryId, Quantity) VALUES (@OrderId, @InventoryId, @Quantity)";
_cmd.Parameters.Clear();
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@OrderId", _identity);
_cmd.Parameters.AddWithValue("@InventoryId", int.Parse(item.SubItems[2].Text));
_cmd.Parameters.AddWithValue("@Quantity", int.Parse(item.SubItems[0].Text));
//Insert OrderDetails data
Data.Database.InsertUpdateDelete(_cmd);
}
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Update the current state of an order
/// </summary>
/// <param name="orderId"></param>
/// <param name="orderState"></param>
public static void UpdateOrderState(int orderId, OrderState orderState)
{
SqlCeCommand _cmd = null;
try
{
//Update OrderState value
int _orderState = (int)orderState;
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "UPDATE Orders SET OrderState = @OrderState WHERE OrderId = @OrderId";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@OrderState", _orderState);
_cmd.Parameters.AddWithValue("@OrderId", orderId);
//Update the order state based on the passed-in enum
Data.Database.InsertUpdateDelete(_cmd);
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Update the amount of Inventory items in stock
/// </summary>
/// <param name="inventoryId"></param>
/// <param name="inStock"></param>
public static void UpdateInStockAmount(int inventoryId, int inStock)
{
SqlCeCommand _cmd = null;
try
{
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "UPDATE Inventory SET InStock = @InStock WHERE InventoryId = @InventoryId";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@InStock", inStock);
_cmd.Parameters.AddWithValue("@InventoryId", inventoryId);
//Update the InStock amount based on the InventoryId
Data.Database.InsertUpdateDelete(_cmd);
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Get the price of an Inventory item
/// </summary>
/// <param name="inventoryId"></param>
/// <returns></returns>
public static decimal GetPrice(int inventoryId)
{
SqlCeCommand _cmd = null;
try
{
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "SELECT Price from Inventory WHERE InventoryId = @InventoryId";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@InventoryId", inventoryId);
//Retrieve the price based on the InventoryId
return (decimal)Data.Database.ReadSingleValue(_cmd);
}
finally
{
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Retrieve a route map in Bitmap form
/// </summary>
/// <param name="height"></param>
/// <param name="width"></param>
/// <param name="address"></param>
/// <param name="city"></param>
/// <param name="state"></param>
/// <returns></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 Data.NetAccess.CallMapPoint(height, width, address, city, state);
}
/// <summary>
/// Save the signature Gif file to a database Image column
/// </summary>
/// <param name="signaturePath"></param>
/// <param name="orderId"></param>
public static void SaveGifToDatabase(string signaturePath, int orderId)
{
SqlCeCommand _cmd = null;
FileStream fs = null;
byte[] imageData;
try
{
//Open Gif file
fs = File.Open(signaturePath, FileMode.Open);
//Create byte array equal to the size of the Gif file
imageData = new byte[fs.Length];
//Write Gif image data to the byte array
fs.Read(imageData, 0, imageData.Length);
//Create a Command object
_cmd = new SqlCeCommand();
//Set the SQL statement
_cmd.CommandText = "UPDATE Orders SET Signature = @Signature WHERE OrderId = @OrderId";
//Add Paramters plus values
_cmd.Parameters.AddWithValue("@Signature", imageData);
_cmd.Parameters.AddWithValue("@OrderId", orderId);
//Add signature byte array to the Orders table
Data.Database.InsertUpdateDelete(_cmd);
}
finally
{
//Close the FileStream object
fs.Close();
//Release Command object resources
_cmd.Dispose();
}
}
/// <summary>
/// Pass in a string seed to return a unique
/// Device Id in the form of a byte array
/// </summary>
/// <param name="appString"></param>
/// <returns></returns>
private static byte[] getDeviceID(string appString)
{
//Create a byte array based on the length of the appString
byte[] _appData = new byte[appString.Length];
//Iterate through the appString
for (int _count = 0; _count < appString.Length; _count++)
{
//Add appString bytes to _appData byte array
_appData[_count] = (byte)appString[_count];
}
int _appDataSize = _appData.Length;
byte[] _deviceOutput = new byte[20];
uint _sizeOut = 20;
//Call the GetDeviceUniqueID function
GetDeviceUniqueID(_appData, _appDataSize, 1, _deviceOutput, out _sizeOut);
return _deviceOutput;
}
#region P/Invokes
[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID([In, Out] byte[] appdata,
int cbApplictionData,
int dwDeviceIDVersion,
[In, Out] byte[] deviceIDOuput,
out uint pcbDeviceIDOutput);
public class MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("CoreDll.dll")]
public static extern void GlobalMemoryStatus
(
MEMORYSTATUS lpBuffer
);
[DllImport("CoreDll.dll")]
public static extern int GetSystemMemoryDivision
(
ref uint lpdwStorePages,
ref uint lpdwRamPages,
ref uint lpdwPageSize
);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -