Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data.SqlClient;
- using System.IO;
- using CommandLine.Utility;
- namespace PosCameraImagesExtract
- {
- public class Program
- {
- static void Main(string[] args)
- {
- System.String errorMessage;
- // Command line parsing
- Arguments CommandLine = new Arguments(args);
- //if server is given use it if not use local
- String strServer = (CommandLine["server"] != null) ? CommandLine["server"] : "(local)";
- //if export path is given use it, if not use default
- String strexportPath = (CommandLine["exportPath"] != null) ? CommandLine["exportPath"] : @"C:\pos\audit\export\camera-images\";
- //check if the path ends with \ if not add \ to the path
- if (strexportPath.EndsWith(@"\") == false)
- {
- Console.WriteLine("You forgot your backslash, adding one now");
- strexportPath += @"\";
- }
- //Check if the path exists, if not try to create it
- if (!Directory.Exists(strexportPath))
- {
- Console.WriteLine("exportPath " + strexportPath + " does not exist!");
- Console.WriteLine("Attempting to create: " + strexportPath);
- try
- {
- Directory.CreateDirectory(strexportPath);
- }
- //if you cant create custom path set path to default, check if it exists if not create it.
- catch (Exception e)
- {
- errorMessage = e.Message;
- Console.WriteLine("Not able to create exportPath: " + strexportPath);
- strexportPath = @"C:\pos\audit\export\camera-images\";
- Console.WriteLine("export path set to default " + strexportPath);
- if (Directory.Exists(strexportPath) == false)
- {
- Directory.CreateDirectory(strexportPath);
- Console.WriteLine("Folder : " +strexportPath+ " successfully created for exportPath");
- }
- }
- }
- string myConnectionString = null;
- if (myConnectionString == null)
- {
- myConnectionString = "Initial Catalog=posimage;Data Source="+ strServer +";Integrated Security=true;";
- //myConnectionString = "Initial Catalog=posimage;Data Source=10.0.0.66;Integrated Security=true;";
- }
- SqlConnection myConnection = new SqlConnection(myConnectionString);
- myConnection.Open();
- DateTime lastAuditDate = DateTime.Now;
- //Export
- int exportCount = getNumOfImages(myConnection);
- if (exportCount > 0)
- {
- Console.WriteLine(exportCount + " Rows to export");
- Console.WriteLine("LastAduitDate: " + lastAuditDate);
- exportRowsToFile(myConnection, lastAuditDate, strexportPath);
- }
- else
- {
- Console.WriteLine("No Data to export");
- }
- //Update posimage.dbo.ticketimageexportlog setlastexportauditdate = newauditdate
- updateLastAudit(myConnection, lastAuditDate);
- myConnection.Close();
- }
- private static void updateLastAudit(SqlConnection myConnection, DateTime lastAuditDate)
- {
- string updateStatement = "UPDATE posimage.dbo.ticketimageexportlog " +
- "SET lastexportauditdate = '" + lastAuditDate.ToString() + "'";
- SqlCommand myCommand = new SqlCommand(updateStatement);
- myCommand.Connection = myConnection;
- myCommand.ExecuteNonQuery();
- }
- private static string ByteArrayToString(byte[] ba)
- {
- StringBuilder hex = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- hex.AppendFormat("{0:x2}", b);
- return hex.ToString().ToUpper();
- }
- private static SqlDataReader sqlReaderfromSelect (SqlConnection myConnection,string mySelectQuery)
- {
- SqlCommand myCommand = new SqlCommand(mySelectQuery);
- myCommand.Connection = myConnection;
- SqlDataReader myReader = myCommand.ExecuteReader();
- return myReader;
- }
- private static int getNumOfImages(SqlConnection myConnection)
- {
- int result = 0;
- string mySelectQuery = "select count(*) from posimage.dbo.TicketImage where auditdate > " +
- "(SELECT lastexportauditdate FROM posimage.dbo.ticketimageexportlog where ticketImages_log = 1) ";
- SqlDataReader myReader = sqlReaderfromSelect(myConnection,mySelectQuery);
- while (myReader.Read())
- {
- result = (int)myReader.GetValue(0);
- }
- myReader.Close();
- return result ;
- }
- private static string getSQLservername(SqlConnection myConnection)
- {
- string result = "";
- string mySelectQuery = "select @@servername";
- SqlDataReader myReader = sqlReaderfromSelect(myConnection, mySelectQuery);
- while (myReader.Read())
- {
- result = myReader.GetValue(0).ToString();
- }
- myReader.Close();
- return result;
- }
- private static void exportRowsToFile (SqlConnection myConnection, DateTime lastAuditDate, String exportPath)
- {
- string serverName = getSQLservername(myConnection);
- string mySelectQuery = "select * from posimage.dbo.TicketImage where auditdate > " +
- "(SELECT lastexportauditdate FROM posimage.dbo.ticketimageexportlog where ticketImages_log = 1) ";
- SqlDataReader myReader = sqlReaderfromSelect(myConnection, mySelectQuery);
- string strExpFile = exportPath + serverName + ".tktimage." + lastAuditDate.ToString("yyyy-MM-ddHHmmss");
- Console.WriteLine("Writing to file: " + strExpFile);
- StreamWriter export = new StreamWriter(strExpFile);
- //header writer
- for (int i = 0; i < myReader.FieldCount; i++)
- {
- export.Write((i == 0 ? "\"" : "|\"") + myReader.GetName(i) + "\"");
- }
- export.WriteLine("");
- //data writer
- while (myReader.Read())
- {
- StringBuilder sbRow = new StringBuilder();
- sbRow.Append("{" + myReader[0].ToString().ToUpper() + "}|");//RecordID
- sbRow.Append(myReader[1].ToString() + "|");//TktNum
- sbRow.Append(myReader[2].ToString() + "|");//Signature
- sbRow.Append(myReader[3].ToString() + "|");//Photo
- sbRow.Append(string.Format("{0:yyyy-MM-dd HH:mm:ss.ffff}", myReader[4]) + "00000|");//AuditDate
- sbRow.Append(ByteArrayToString((byte[])myReader[5]));//image
- export.WriteLine(sbRow); //write line to file
- }
- myReader.Close();
- export.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement