Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using CommandLine.Utility;
  7.  
  8.  
  9. namespace PosCameraImagesExtract
  10. {
  11.     public class Program
  12.     {
  13.        
  14.         static void Main(string[] args)
  15.         {
  16.                 System.String errorMessage;
  17.  
  18.                 // Command line parsing
  19.                 Arguments CommandLine = new Arguments(args);
  20.  
  21.                 //if server is given use it if not use local
  22.                 String strServer = (CommandLine["server"] != null) ? CommandLine["server"] : "(local)";
  23.                 //if export path is given use it, if not use default
  24.                 String strexportPath = (CommandLine["exportPath"] != null) ? CommandLine["exportPath"] : @"C:\pos\audit\export\camera-images\";
  25.             //check if the path ends with \ if not add \ to the path  
  26.             if (strexportPath.EndsWith(@"\") == false)
  27.                 {
  28.                     Console.WriteLine("You forgot your backslash, adding one now");
  29.                     strexportPath += @"\";
  30.                 }  
  31.             //Check if the path exists, if not  try to create it
  32.             if (!Directory.Exists(strexportPath))
  33.                 {
  34.                     Console.WriteLine("exportPath " + strexportPath + " does not exist!");
  35.                     Console.WriteLine("Attempting to create: " + strexportPath);
  36.                     try
  37.                     {
  38.                         Directory.CreateDirectory(strexportPath);
  39.                     }
  40.                     //if you cant create custom path set path to default, check if it exists if not create it.
  41.                     catch (Exception e)
  42.                     {
  43.                         errorMessage = e.Message;
  44.  
  45.                         Console.WriteLine("Not able to create exportPath: " + strexportPath);
  46.                         strexportPath = @"C:\pos\audit\export\camera-images\";
  47.                         Console.WriteLine("export path set to default " + strexportPath);
  48.                         if (Directory.Exists(strexportPath) == false)
  49.                         {  
  50.                             Directory.CreateDirectory(strexportPath);
  51.                             Console.WriteLine("Folder : " +strexportPath+ " successfully created for exportPath");
  52.                         }
  53.                     }
  54.                 }
  55.                 string myConnectionString = null;
  56.                 if (myConnectionString == null)
  57.                 {
  58.                     myConnectionString = "Initial Catalog=posimage;Data Source="+ strServer +";Integrated Security=true;";
  59.                     //myConnectionString = "Initial Catalog=posimage;Data Source=10.0.0.66;Integrated Security=true;";
  60.                 }
  61.                 SqlConnection myConnection = new SqlConnection(myConnectionString);
  62.                 myConnection.Open();
  63.                 DateTime lastAuditDate = DateTime.Now;
  64.                 //Export
  65.                 int exportCount = getNumOfImages(myConnection);
  66.                 if (exportCount > 0)
  67.                 {
  68.                     Console.WriteLine(exportCount + " Rows to export");
  69.                     Console.WriteLine("LastAduitDate: " + lastAuditDate);
  70.                     exportRowsToFile(myConnection, lastAuditDate, strexportPath);
  71.                 }
  72.                 else
  73.                 {
  74.                     Console.WriteLine("No Data to export");
  75.                 }
  76.                 //Update posimage.dbo.ticketimageexportlog setlastexportauditdate = newauditdate
  77.                 updateLastAudit(myConnection, lastAuditDate);
  78.                 myConnection.Close();
  79.             }
  80.         private static void updateLastAudit(SqlConnection myConnection, DateTime lastAuditDate)
  81.         {
  82.             string updateStatement = "UPDATE posimage.dbo.ticketimageexportlog " +
  83.             "SET lastexportauditdate = '"  + lastAuditDate.ToString() + "'";
  84.             SqlCommand myCommand = new SqlCommand(updateStatement);
  85.             myCommand.Connection = myConnection;
  86.             myCommand.ExecuteNonQuery();
  87.         }
  88.         private static string ByteArrayToString(byte[] ba)
  89.         {
  90.             StringBuilder hex = new StringBuilder(ba.Length * 2);
  91.             foreach (byte b in ba)
  92.                 hex.AppendFormat("{0:x2}", b);
  93.             return hex.ToString().ToUpper();
  94.         }
  95.         private static SqlDataReader sqlReaderfromSelect (SqlConnection myConnection,string mySelectQuery)
  96.         {
  97.             SqlCommand myCommand = new SqlCommand(mySelectQuery);
  98.             myCommand.Connection = myConnection;
  99.             SqlDataReader myReader = myCommand.ExecuteReader();
  100.             return myReader;
  101.         }
  102.         private static int getNumOfImages(SqlConnection myConnection)
  103.         {
  104.             int result = 0;
  105.             string mySelectQuery = "select count(*) from posimage.dbo.TicketImage where auditdate > " +
  106.                                     "(SELECT lastexportauditdate FROM posimage.dbo.ticketimageexportlog where ticketImages_log = 1) ";
  107.             SqlDataReader myReader = sqlReaderfromSelect(myConnection,mySelectQuery);
  108.             while (myReader.Read())
  109.             {
  110.                        result = (int)myReader.GetValue(0);
  111.             }
  112.             myReader.Close();
  113.             return result ;
  114.         }
  115.         private static string getSQLservername(SqlConnection myConnection)
  116.         {
  117.             string result = "";
  118.             string mySelectQuery = "select @@servername";
  119.             SqlDataReader myReader = sqlReaderfromSelect(myConnection, mySelectQuery);
  120.             while (myReader.Read())
  121.             {
  122.                 result = myReader.GetValue(0).ToString();
  123.             }
  124.             myReader.Close();
  125.             return result;
  126.         }
  127.        
  128.         private static void exportRowsToFile (SqlConnection myConnection, DateTime lastAuditDate, String exportPath)
  129.         {
  130.             string serverName = getSQLservername(myConnection);
  131.             string mySelectQuery = "select * from posimage.dbo.TicketImage where auditdate > " +
  132.                 "(SELECT lastexportauditdate FROM posimage.dbo.ticketimageexportlog where ticketImages_log = 1) ";
  133.             SqlDataReader myReader = sqlReaderfromSelect(myConnection, mySelectQuery);
  134.             string strExpFile = exportPath + serverName + ".tktimage." + lastAuditDate.ToString("yyyy-MM-ddHHmmss");
  135.             Console.WriteLine("Writing to file: " + strExpFile);
  136.             StreamWriter export = new StreamWriter(strExpFile);
  137.  
  138.             //header writer
  139.             for (int i = 0; i < myReader.FieldCount; i++)
  140.             {
  141.                 export.Write((i == 0 ? "\"" : "|\"") + myReader.GetName(i) + "\"");
  142.             }
  143.             export.WriteLine("");
  144.  
  145.             //data writer
  146.             while (myReader.Read())
  147.             {
  148.                 StringBuilder sbRow = new StringBuilder();
  149.  
  150.                 sbRow.Append("{" + myReader[0].ToString().ToUpper() + "}|");//RecordID
  151.                 sbRow.Append(myReader[1].ToString() + "|");//TktNum
  152.                 sbRow.Append(myReader[2].ToString() + "|");//Signature
  153.                 sbRow.Append(myReader[3].ToString() + "|");//Photo
  154.                 sbRow.Append(string.Format("{0:yyyy-MM-dd HH:mm:ss.ffff}", myReader[4]) + "00000|");//AuditDate
  155.                 sbRow.Append(ByteArrayToString((byte[])myReader[5]));//image
  156.                 export.WriteLine(sbRow); //write line to file
  157.  
  158.                
  159.             }
  160.             myReader.Close();
  161.             export.Close();
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement